题目描述
blablabla
样例
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
ans = True
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
self.helper(root)
return self.ans
def helper(self, root):
if not root:
return 0
left = self.helper(root.left)
right = self.helper(root.right)
if abs(right - left) > 1:
self.ans = False
return right + 1 if right > left else left + 1