给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
class TreeNode:标签:node,right,helper,self,110,二叉树,LeetCode,left From: https://blog.51cto.com/u_13866182/5857537
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
self.res = True
def helper(node):
if node is None:
return 0
left = helper(node.left) + 1
right = helper(node.right) + 1
if abs(left - right) > 1:
self.res = False
return max(left, right)
helper(root)
return self.res