题解 | #判断是不是平衡二叉树#

判断是不是平衡二叉树

https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param pRoot TreeNode类 
# @return bool布尔型
#
class Solution:
    flag = 1
    def getDepth(self , this : TreeNode):
        if not this:
            return 0
        return 1 + max(self.getDepth(this.left) , self.getDepth(this.right))
    def DLR(self , this : TreeNode):
        if this:
            if abs(self.getDepth(this.left) - self.getDepth(this.right)) > 1:
                self.flag = 0
                return 
            self.DLR(this.left)
            self.DLR(this.right)
            
    def IsBalanced_Solution(self , pRoot: TreeNode) -> bool:
        # write code here
        self.DLR(pRoot)
        if self.flag == 1:
            return True
        else:
            return False

全部评论

相关推荐

02-03 12:20
门头沟学院 Java
一天代码十万三:你这个简历东西太杂了,根本看不出你想投什么岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务