Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ece29c7

Browse files
🐱(tree): 110. 平衡二叉树
1. 补充自顶向下解法 2. 补充 golang 题解
1 parent 2668785 commit ece29c7

File tree

1 file changed

+109
-21
lines changed

1 file changed

+109
-21
lines changed

‎docs/data-structure/tree/recursion/README.md‎

Lines changed: 109 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -364,35 +364,123 @@ func findIndex(array []int, num int) int {
364364

365365
[原题链接](https://leetcode-cn.com/problems/balanced-binary-tree/description/)
366366

367-
### 思路
368-
369367
[104](/tree/104.md) 的套路一样,加上对比逻辑而已。
370368

369+
### 解一:自顶向下
370+
371+
对每个节点都计算它左右子树的高度,判断是否为平衡二叉树。
372+
371373
```python
372-
class Solution(object):
373-
374-
result = True
375-
376-
def isBalanced(self, root):
374+
# Definition for a binary tree node.
375+
# class TreeNode:
376+
# def __init__(self, x):
377+
# self.val = x
378+
# self.left = None
379+
# self.right = None
380+
381+
class Solution:
382+
def isBalanced(self, root: TreeNode) -> bool:
383+
if root is None:
384+
return True
385+
left_depth = self.helper(root.left, 0)
386+
right_depth = self.helper(root.right, 0)
387+
if abs(left_depth - right_depth) > 1:
388+
# 不平衡
389+
return False
390+
return self.isBalanced(root.left) and self.isBalanced(root.right)
391+
392+
def helper(self, root, depth):
377393
"""
378-
:type root: TreeNode
379-
:rtype: bool
394+
返回节点高度
380395
"""
381-
self.depth(root)
382-
return self.result
383-
384-
def depth(self, root):
385396
if root is None:
386-
return 0
387-
else:
388-
left_depth = self.depth(root.left)
389-
right_depth = self.depth(root.right)
390-
if (abs(left_depth - right_depth) > 1):
391-
self.result = False
392-
393-
return max(left_depth, right_depth) + 1
397+
return depth
398+
left_depth = self.helper(root.left, depth + 1)
399+
right_depth = self.helper(root.right, depth + 1)
400+
return max(left_depth, right_depth)
394401
```
395402

403+
- 时间复杂度:$O(nlogn)$。最差情况需要遍历树的所有节点,判断每个节点的最大高度又需要遍历该节点的所有子节点。详见[题解中的复杂度分析](https://leetcode-cn.com/problems/balanced-binary-tree/solution/ping-heng-er-cha-shu-by-leetcode/)
404+
- 空间复杂度:$O(n)$。如果树完全倾斜(退化成链),递归栈将包含所有节点。
405+
406+
### 解二:自底向上
407+
408+
自顶向下的高度计算存在大量冗余,每次计算高度时,同时都要计算子树的高度。
409+
410+
从底自顶返回二叉树高度,如果某子树不是平衡树,提前进行枝剪。
411+
412+
<!-- tabs:start -->
413+
414+
#### **Python**
415+
416+
```python
417+
# Definition for a binary tree node.
418+
# class TreeNode:
419+
# def __init__(self, x):
420+
# self.val = x
421+
# self.left = None
422+
# self.right = None
423+
424+
class Solution:
425+
res = True
426+
def isBalanced(self, root: TreeNode) -> bool:
427+
self.helper(root, 0)
428+
return self.res
429+
430+
def helper(self, root, depth):
431+
if root is None:
432+
return depth
433+
left_depth = self.helper(root.left, depth + 1)
434+
right_depth = self.helper(root.right, depth + 1)
435+
if abs(left_depth - right_depth) > 1:
436+
self.res = False
437+
# 提前返回
438+
return 0
439+
return max(left_depth, right_depth)
440+
```
441+
442+
- 时间复杂度:$O(n)$。n 为树的节点数,最坏情况需要遍历所有节点。
443+
- 空间复杂度:$O(n)$。完全不平衡时需要的栈空间。
444+
445+
#### **Go**
446+
447+
```go
448+
/**
449+
* Definition for a binary tree node.
450+
* type TreeNode struct {
451+
* Val int
452+
* Left *TreeNode
453+
* Right *TreeNode
454+
* }
455+
*/
456+
var res bool
457+
458+
func isBalanced(root *TreeNode) bool {
459+
res = true
460+
helper(root, 0)
461+
return res
462+
}
463+
464+
func helper(root *TreeNode, depth int) int {
465+
if root == nil {
466+
return depth
467+
}
468+
leftDepth := helper(root.Left, depth + 1)
469+
rightDepth := helper(root.Right, depth + 1)
470+
if leftDepth - rightDepth > 1 || leftDepth - rightDepth < -1 {
471+
res = false
472+
return 0
473+
}
474+
if leftDepth > rightDepth {
475+
return leftDepth
476+
} else {
477+
return rightDepth
478+
}
479+
}
480+
```
481+
482+
<!-- tabs:end -->
483+
396484

397485
## 111. 二叉树的最小深度
398486

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /