简体   繁体   English

关于 PYTHON 3 全局变量和递归 function 中的更改变量,leetcode 104

[英]about PYTHON 3 global variable and change variable in a recursive function, leetcode 104

leetcode 104. Maximum Depth of Binary Tree leetcode 104. 二叉树的最大深度

My first try is write like this, but the final return value is 0 although in the recursive function depth increased to correct value.我的第一次尝试是这样写,但最终返回值为 0 尽管在递归 function 深度增加到正确值。

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        
        depth = 0
        
        def mxdp(root,depth):   
            if root:
                depth += 1
            
            print(root.val,depth)
            
            if root.left:
                mxdp(root.left,depth)
            if root.right:
                mxdp(root.right,depth)
        
        mxdp(root,depth)
        print(depth)
        return depth

Then I try to use global variable after read some online article, below is my 2nd version, but it give me an error: name 'depth' is not defined at this line: depth += 1然后我在阅读一些在线文章后尝试使用全局变量,下面是我的第二个版本,但它给了我一个错误:name 'depth' is not defined at this line: depth += 1

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        
        depth = 0
        
        def mxdp(root): 
            global depth
            if root:
                depth += 1  
            if root.left:
                mxdp(root.left)
            if root.right:
                mxdp(root.right)
        
        mxdp(root) 
        return depth        

Could you please teach me why none of these two ways are not working?你能教我为什么这两种方法都不起作用吗? How to write correctly?如何正确书写? Thanks!谢谢!

For your second example you need to use self.depth or Solution.depth instead of just depth in order to access the depth variable.对于您的第二个示例,您需要使用self.depthSolution.depth而不仅仅是depth才能访问depth变量。

Your first example you would need to use the nonlocal keyword in order to update the variable that is out of scope.您的第一个示例需要使用 nonlocal 关键字来更新nonlocal之外的变量。

def mxdp(root):
    nonlocal depth
    ...

Both of these will still return the incorrect value however because you are adding 1 to the depth at each iteration, and most trees have more than just one branch.然而,这两个仍然会返回不正确的值,因为您在每次迭代时将深度加 1,并且大多数树不止一个分支。

An alternative to both of these approaches which avoids using global or out of scope variables would be to create a separate method that takes the root node and an integer and use the integer to keep track of the depth value, returning that value once you have finally reached the bottom and making sure you keep the largest on your way back out.避免使用全局或 scope 变量的这两种方法的替代方法是创建一个单独的方法,该方法采用根节点和 Z157DB7DF53002357DB7DF530023575515D366C9B672E8Z 并使用 integer 并使用 Z157DB7DF530023575515D366C9B672E8到达底部并确保在退出的过程中保持最大的。

For example:例如:

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        return self.recurse(root, 1)
        
    
    def recurse(self, root, depth):
        depth_left = depth_right = 0
        if root.left is None and root.right is None:
            return depth
        if root.left is not None:
            depth_left = self.recurse(root.left, depth + 1)
        if root.right is not None:
            depth_right = self.recurse(root.right, depth + 1)
        return max([depth_left, depth_right])

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

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