题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
1 class Solution: 2 def IsBalanced_Solution(self, pRoot): 3 # write code here 4 if pRoot == None: 5 return True 6 if abs(self.Depth(pRoot.left)-self.Depth(pRoot.right))>1: 7 return False 8 return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right) 9 def Depth(self,pRoot): 10 if pRoot==None: 11 return 0 12 left=self.Depth(pRoot.left) 13 right=self.Depth(pRoot.right) 14 return max(left+1,right+1)
2019-12-25 16:47:38