Closed chengchengxu15 closed 3 years ago
方法一: 递归 recursive: 检测每次左右两边是否是Binary Tree,是的话返回高度+1
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def isbalanced_height(root):
if root == None:
return 0
left_height = isbalanced_height(root.left)
right_height = isbalanced_height(root.right)
if left_height == -1 or right_height == -1 or abs(left_height - right_height)>1:
return -1
return max(left_height,right_height) +1
return isbalanced_height(root)>=0
方法二: 检查每一个检点左右两个的高度是否满足条件。有点蠢。。。
https://leetcode.com/problems/balanced-binary-tree/
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.