39. The balanced binary tree (python)

Title Description

Input binary tree, the binary tree is determined whether a balanced binary tree.
 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

Guess you like

Origin www.cnblogs.com/NPC-assange/p/12097603.html