Update 0110.平衡二叉树.md

This commit is contained in:
jianghongcheng
2023-05-04 21:49:12 -05:00
committed by GitHub
parent 3d59f732e0
commit 0f5b0250a3

View File

@ -532,6 +532,24 @@ class Solution:
else:
return 1 + max(left_height, right_height)
```
递归法精简版:
```python
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
return self.height(root) != -1
def height(self, node: TreeNode) -> int:
if not node:
return 0
left = self.height(node.left)
if left == -1:
return -1
right = self.height(node.right)
if right == -1 or abs(left - right) > 1:
return -1
return max(left, right) + 1
```
迭代法: