题解 | #平衡二叉树#
平衡二叉树
http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
-- coding:utf-8 --
class TreeNode:
def init(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def IsBalanced(self,pRoot):
if pRoot is None:
return True,0
else:
booll,depthl=self.IsBalanced(pRoot.left)
boolr,depthr=self.IsBalanced(pRoot.right)
if booll and boolr and abs(depthl-depthr)<=1:
return True,max(depthl,depthr)+1
else:
return False,max(depthl,depthr)+1
def IsBalanced_Solution(self,pRoot):
return self.IsBalanced(pRoot)[0]