global是全局變量為什么改變不了tell的值為1
# 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
標(biāo)簽: