题目:
题解:
class Solution:def isBalanced(self, root: TreeNode) -> bool:def height(root: TreeNode) -> int:if not root:return 0leftHeight = height(root.left)rightHeight = height(root.right)if leftHeight == -1 or rightHeight == -1 or abs(leftHeight - rightHeight) > 1:return -1else:return max(leftHeight, rightHeight) + 1return height(root) >= 0