平衡二叉树
左右子树都为空,或者左右子树的深度之差不超过1,且左右子树也为平衡二叉树
判断是否为平衡二叉树方法(剑指offer)
`
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def IsBalanced_Solution(self, pRoot):
# write code here
if pRoot==None:
return True
if abs(self.get_depth(pRoot.left)-self.get_depth(pRoot.right))>1:
return False
else:
return True
def get_depth(self,pRoot):
if pRoot==None:
return 0
return max(self.get_depth(pRoot.left),self.get_depth(pRoot.right))+1`