欢迎光临散文网 会员登陆 & 注册

global是全局变量为什么改变不了tell的值为1

2023-08-25 13:50 作者:晓吐灾子  | 我要投稿

# global tell

class Solution:

    def isBalanced(self, root: TreeNode) -> bool:

       

        tell=0

        def f1(root:TreeNode):

            nonlocal tell

            if root is None:

                return 0

            left=f1(root.left)

            right=f1(root.right)

            # print(type(right))

            if abs(left -right)>1:

                tell=1

                return 2


            else :

                return max(left,right)+1

        f1(root)

        if tell:

            return False

        else:

            return True



global是全局变量为什么改变不了tell的值为1的评论 (共 条)

分享到微博请遵守国家法律