题解 | #平衡二叉树#

平衡二叉树

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_Solution(self, pRoot):
        # write code here
        def helper(root):
            if not root:
                return 0
            ldep = helper(root.left)
            rdep = helper(root.right)
            sub = abs(ldep-rdep)
            if ldep == -1 or rdep == -1:
                return -1
            if sub > 1:
                return -1
            else:
                return max(ldep, rdep) + 1
        return helper(pRoot) != -1
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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