二叉树遍历到链表

时间:2017-04-19 23:17:42

标签: algorithm python-3.x recursion linked-list binary-tree

我正在读我的考试,我无法弄清楚如何回答这个问题。

创建一个函数,返回二叉树的遍历遍历的链表。您不能使用任何容器来存储您的数据,它必须是递归的。

所以我不能进行帖子遍历,把它放在一个列表中然后迭代它来制作一个链表。

还有另一个问题是做同样的事情,但是为了顺序遍历,这就是解决方案。

有两种解决方案。一个我不理解的和那个的明确版本

这是主要解决方案

def inorder(root: BTNode) -> LLNode:
"""Return the first node in a linked list that contains every value from the
binary tree rooted at root, listed according to an inorder traversal.

>>> b = BTNode(1, BTNode(2), BTNode(3))
>>> repr(inorder(b))
'LLNode(2, LLNode(1, LLNode(3)))'
>>> b2 = BTNode(4, BTNode(5))
>>> b3 = BTNode(7, b, b2)
>>> str(inorder(b3))
'2 -> 1 -> 3 -> 7 -> 5 -> 4'
>>> # from the handout...
>>> left = BTNode('B', None, BTNode('D', BTNode('G')))
>>> right = BTNode('C', BTNode('E'), BTNode('F'))
>>> root = BTNode('A', left, right)
>>> str(inorder(root))
'B -> G -> D -> A -> E -> C -> F'
"""
return _inorder(root)[0]


def _inorder(root: BTNode) -> (LLNode, LLNode):
"""Return the first and last nodes in a linked list that contains every
value from the binary tree rooted at root, listed according to an inorder
traversal.
"""
if root:
    head_left, tail_left = _inorder(root.left)
    head_right, tail_right = _inorder(root.right)
    node_root = LLNode(root.item, head_right)
    if tail_left:
        tail_left.link = node_root
    return head_left or node_root, tail_right or node_root
else:
    return None, None

清除解决方案

def inorder(root: BTNode) -> LLNode:
"""Return the first node in a linked list that contains every value from the
binary tree rooted at root, listed according to an inorder traversal.

>>> b = BTNode(1, BTNode(2), BTNode(3))
>>> repr(inorder(b))
'LLNode(2, LLNode(1, LLNode(3)))'
>>> b2 = BTNode(4, BTNode(5))
>>> b3 = BTNode(7, b, b2)
>>> str(inorder(b3))
'2 -> 1 -> 3 -> 7 -> 5 -> 4'
>>> # from the handout...
>>> left = BTNode('B', None, BTNode('D', BTNode('G')))
>>> right = BTNode('C', BTNode('E'), BTNode('F'))
>>> root = BTNode('A', left, right)
>>> str(inorder(root))
'B -> G -> D -> A -> E -> C -> F'
"""
return _inorder(root)[0] # what must this first item represent?


def _inorder(root: BTNode) -> (LLNode, LLNode): # what are these 1st and 2nd things?
"""Return the first and last nodes in a linked list that contains every
value from the binary tree rooted at root, listed according to an inorder
traversal.

>>> left = BTNode('B', None, BTNode('D', BTNode('G')))
>>> right = BTNode('C', BTNode('E'), BTNode('F'))
>>> root = BTNode('A', left, right)
>>> str(inorder(root))
'B -> G -> D -> A -> E -> C -> F'
"""    

if not root:
    return None, None

    else:
    # Start off by making a new node of our item, with None for a link
    # Obviously we will need to replace that None if we have a right branch
    new_node = LLNode(root.item)

    # Recursive call on right branch gives us its head and tail        
    right_head, right_tail = _inorder(root.right)

    # The link on our new node should be the right head, even if it's None
    new_node.link = right_head

    # Ultimately the tail for this whole node will be the rightmost tail
    # If there is no right side, though, this node is the rightmost tail
    if not right_tail:
        right_tail = new_node

    # Recursive call on left branch gives us its head and tail
    left_head, left_tail = _inorder(root.left)

    # If there is a left tail, we should string our current node to the end
    if left_tail:
        left_tail.link = new_node

    # Ultimately the head for this whole node will be the leftmost head
    # If there is no left head, though, this node is the leftmost head
    if not left_head:
        left_head = new_node

     # Return the leftmost head and the rightmost tail
    return left_head, right_tail

1 个答案:

答案 0 :(得分:0)

我不确定是什么让你无法理解主要解决方案。评论很好地解释了这一点。在任何情况下,对_inorder(root.left)的递归调用会使左子树变平,并返回结果列表的头部和尾部。同样,对_inorder(root.right)的递归调用会使右侧子树变平。现在你有两个列表,

head_left-> ... ->tail_left
head_right-> ... ->tail_right

用根缝合它们,

head_left-> ... ->tail_left->root->head_right-> ... ->tail_right

并返回结果列表

head_left-> ... ->tail_right

要实现后置订单,请按

中的方式缝制它们
head_left-> ... ->tail_left->head_right-> ... ->tail_right->root

并返回

head_left-> ... ->root