Periksa apakah pohon biner seimbang Python
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def depth(root):
if not root:
return 0
return max(depth(root.left), depth(root.right)) + 1
def solution(root):
if not root:
return True
if abs(depth(root.left) - depth(root.right)) > 1:
return False
return solution(root.left) and solution(root.right)
return solution(root)
Bacem OBEY