Relative Content

Tag Archive for pythonfunctionnested

using variable out of nested function

class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: balanced = [True] def node_height(root): if not root or not balanced[0]: return 0 left_height = node_height(root.left) right_height = node_height(root.right) if abs(left_height – right_height) > 1: balanced[0] = False return 0 return 1 + max(left_height, right_height) node_height(root) return balanced[0] I understand why the code above works but when […]