题解 | #平衡二叉树#

平衡二叉树

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]

全部评论

相关推荐

本神尊:看来是没招到小红薯上的人
点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务