题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def getDepth(self,Root):
if Root == None:
return 0
lDepth = Solution.getDepth(self,Root.left)
rDepth = Solution.getDepth(self,Root.right)
return max(lDepth,rDepth)+1
def IsBalanced_Solution(self, pRoot):
# write code here
if not pRoot:
return True
lDepth = Solution.getDepth(self,pRoot.left)
rDepth = Solution.getDepth(self,pRoot.right)
diff = lDepth - rDepth
if diff < -1 or diff > 1:
return False
return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)