# 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):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
isLB = self.isBalanced(root.left)
isRB = self.isBalanced(root.right)
if isLB and isRB:
leftHeight = self.geiHeight(root.left)
rightHeight = self.geiHeight(root.right)
if abs(leftHeight -rightHeight) <= 1:
return True
return False
def geiHeight(self,root):
if not root:
return 0
return max(self.geiHeight(root.left),self.geiHeight(root.right))+1