class Solution(object):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
# 求左子树深度
leftDenth = self.treeDepth(root.left)
rightDenth = self.treeDepth(root.right)
resLeft = self.isBalanced(root.left)
resRight = self.isBalanced(root.right)
if resLeft and resRight and abs(leftDenth-rightDenth)<=1:
return True
else:
return False
# 求二叉树的深度
def treeDepth(self, root):
if not root:
return 0
depthLeft = self.treeDepth(root.left)
depthRight = self.treeDepth(root.right)
return depthLeft + 1 if depthLeft > depthRight else depthRight + 1