思路
计算树的高度,看每个节点的左子树和右子树的高度差是否超过1
class Solution(object):
def isBalanced(self, root):
def height(root):
if not root:
return 0
if not root.left and not root.right:
return 1
return max(height(root.left), height(root.right)) + 1
if not root:
return True
return abs(height(root.left) - height(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)