简体   繁体   English

如何理解二叉树中的以下递归函数?

[英]How to comprehend the following recursive function in binary tree?

The following is a Binary search function (a root has a left and a right child) which I don't quite comprehend.下面是一个我不太理解的二进制搜索函数(一个根有一个左孩子和一个右孩子)。 In the code it returns a list that is the longest path in the binary tree.在代码中,它返回一个列表,它是二叉树中最长的路径。 However for the part: return_path_left = list_longest_path(node.left) return_path_right = list_longest_path(node.right) if len(return_path_left) > len(return_path_right):但是对于部分: return_path_left = list_longest_path(node.left) return_path_right = list_longest_path(node.right) if len(return_path_left) > len(return_path_right):

how can you compare two recursive call?你怎么能比较两个递归调用? For example, if the tree is例如,如果树是

1 / \\ 2 list_longest_path(node.right) will surely return [] . 1 / \\ 2 list_longest_path(node.right)肯定会返回[] But how do you compare list_longest_path(2) with [] ?但是你如何比较list_longest_path(2)[]

Someone help will be great.有人帮助会很棒。

def list_longest_path(node):
    """
    List the data in a longest path of node.

    @param BinaryTree|None node: tree to list longest path of
    @rtype: list[object]

    >>> list_longest_path(None)
    []
    >>> list_longest_path(BinaryTree(5))
    [5]
    >>> b1 = BinaryTree(7)
    >>> b2 = BinaryTree(3, BinaryTree(2), None)
    >>> b3 = BinaryTree(5, b2, b1)
    >>> list_longest_path(b3)
    [5, 3, 2]
    """
    if node is None:
        return []
    else:
        return_path_left = list_longest_path(node.left)
        return_path_right = list_longest_path(node.right)
        if len(return_path_left) > len(return_path_right):
            return [node.data] + return_path_left
        else:
            return [node.data] + return_path_right

list_longest_path(node.right) will surely return []. list_longest_path(node.right) 肯定会返回 []。 But how do you compare list_longest_path(2) with []?但是你如何比较 list_longest_path(2) 和 []?

When a recursive call like list_longest_path(2) is encountered, it gets pushed onto the call stack.当遇到像 list_longest_path(2) 这样的递归调用时,它会被推送到调用堆栈上。 As the call stack is a stack [and is thus last in first out] the current stack frame is halted and list_longest_path(2) is evaluated.由于调用堆栈是一个堆栈[因此是后进先出],当前堆栈帧被暂停并评估 list_longest_path(2)。

list_longest_path(2) is evaluated like so: list_longest_path(2) 的评估方式如下:

As both left and right nodes are None, return_path_left = [];由于左右节点都是None,return_path_left = []; return_path_right = []; return_path_right = []; So list_longest_path(2) = [2] + [] = [2]所以 list_longest_path(2) = [2] + [] = [2]

Then the list_longest_path(2) stackframe is popped off the stack and the program resumes execution in the previous stackframe.然后将 list_longest_path(2) 堆栈帧从堆栈中弹出,程序在前一个堆栈帧中继续执行。 We now have a simple value for list_longest_path(2) = [2] We then finish up the execution of this function len([2]) > len([]) so list_longest_path(1) = [1] + [2] = [1,2]我们现在有一个简单的值 list_longest_path(2) = [2] 然后我们完成这个函数的执行 len([2]) > len([]) 所以 list_longest_path(1) = [1] + [2] = [1,2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM