题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
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