Skip to content

Commit 88306e1

Browse files
committed
✨feat: add 剑指 二叉树
1 parent bf49e1e commit 88306e1

7 files changed

+645
-0
lines changed

Index/BFS.md

+4
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
| [1022. 从根到叶的二进制数之和](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solution/by-ac_oier-1905/) | 简单 | 🤩🤩🤩🤩 |
2323
| [1161. 最大层内元素和](https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/solution/by-ac_oier-aemz/) | 中等 | 🤩🤩🤩🤩 |
2424
| [1609. 奇偶树](https://leetcode-cn.com/problems/even-odd-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/even-odd-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-d-kuyi/) | 中等 | 🤩🤩🤩🤩🤩 |
25+
| [剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/solution/by-ac_oier-a9n5/) | 中等 | 🤩🤩🤩🤩 |
26+
| [剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/solution/by-ac_oier-s9jz/) | 简单 | 🤩🤩🤩 |
27+
| [剑指 Offer 32 - III. 从上到下打印二叉树 III](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/solution/by-ac_oier-98od/) | 中等 | 🤩🤩🤩🤩🤩 |
28+
| [剑指 Offer 34. 二叉树中和为某一值的路径](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/solution/by-ac_oier-3ehr/) | 中等 | 🤩🤩🤩🤩🤩 |
2529

Index/DFS.md

+4
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@
5252
| [1766. 互质树](https://leetcode-cn.com/problems/tree-of-coprimes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/tree-of-coprimes/solution/bu-tai-yi-yang-de-dfs-ji-lu-suo-you-zui-d3xeu/) | 困难 | 🤩🤩🤩🤩 |
5353
| [2044. 统计按位或能得到最大值的子集数目](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/solution/by-ac_oier-dos6/) | 困难 | 🤩🤩🤩🤩 |
5454
| [面试题 04.06. 后继者](https://leetcode.cn/problems/successor-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/successor-lcci/solution/by-ac_oier-xib5/) | 中等 | 🤩🤩🤩🤩🤩 |
55+
| [剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/solution/by-ac_oier-a9n5/) | 中等 | 🤩🤩🤩🤩 |
56+
| [剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/solution/by-ac_oier-s9jz/) | 简单 | 🤩🤩🤩 |
57+
| [剑指 Offer 32 - III. 从上到下打印二叉树 III](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/solution/by-ac_oier-98od/) | 中等 | 🤩🤩🤩🤩🤩 |
58+
| [剑指 Offer 34. 二叉树中和为某一值的路径](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/solution/by-ac_oier-3ehr/) | 中等 | 🤩🤩🤩🤩🤩 |
5559

Index/二叉树.md

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
| [1022. 从根到叶的二进制数之和](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solution/by-ac_oier-1905/) | 简单 | 🤩🤩🤩🤩 |
2020
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
2121
| [1305. 两棵二叉搜索树中的所有元素](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/solution/by-ac_oier-c8fv/) | 中等 | 🤩🤩🤩🤩 |
22+
| [剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/solution/by-ac_oier-a9n5/) | 中等 | 🤩🤩🤩🤩 |
23+
| [剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/solution/by-ac_oier-s9jz/) | 简单 | 🤩🤩🤩 |
24+
| [剑指 Offer 32 - III. 从上到下打印二叉树 III](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/solution/by-ac_oier-98od/) | 中等 | 🤩🤩🤩🤩🤩 |
25+
| [剑指 Offer 34. 二叉树中和为某一值的路径](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/solution/by-ac_oier-3ehr/) | 中等 | 🤩🤩🤩🤩🤩 |
2226
| [剑指 Offer 37. 序列化二叉树](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/gong-shui-san-xie-er-cha-shu-de-xu-lie-h-n89a/) | 困难 | 🤩🤩🤩🤩🤩 |
2327
| [面试题 04.06. 后继者](https://leetcode.cn/problems/successor-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/successor-lcci/solution/by-ac_oier-xib5/) | 中等 | 🤩🤩🤩🤩🤩 |
2428

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/solution/by-ac_oier-a9n5/)** ,难度为 **中等**
4+
5+
Tag : 「二叉树」、「DFS」、「BFS」、「递归」、「迭代」
6+
7+
8+
9+
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
10+
11+
例如:
12+
13+
给定二叉树: `[3,9,20,null,null,15,7]`,
14+
```
15+
3
16+
/ \
17+
9 20
18+
/ \
19+
15 7
20+
```
21+
返回:
22+
```
23+
[3,9,20,15,7]
24+
```
25+
26+
提示:
27+
* `节点总数 <= 1000`
28+
29+
---
30+
31+
### 迭代 - BFS
32+
33+
使用「迭代」进行求解是容易的,只需使用常规的 `BFS` 方法进行层序遍历即可。
34+
35+
Java 代码:
36+
```Java
37+
class Solution {
38+
public int[] levelOrder(TreeNode root) {
39+
List<Integer> list = new ArrayList<>();
40+
Deque<TreeNode> d = new ArrayDeque<>();
41+
if (root != null) d.addLast(root);
42+
while (!d.isEmpty()) {
43+
TreeNode t = d.pollFirst();
44+
list.add(t.val);
45+
if (t.left != null) d.addLast(t.left);
46+
if (t.right != null) d.addLast(t.right);
47+
}
48+
int n = list.size();
49+
int[] ans = new int[n];
50+
for (int i = 0; i < n; i++) ans[i] = list.get(i);
51+
return ans;
52+
}
53+
}
54+
```
55+
TypeScript 代码:
56+
```TypeScript
57+
function levelOrder(root: TreeNode | null): number[] {
58+
let he = 0, ta = 0
59+
const ans: number[] = new Array<number>()
60+
const d: TreeNode[] = new Array<TreeNode>()
61+
if (root != null) d[ta++] = root
62+
while (he < ta) {
63+
const t = d[he++]
64+
ans.push(t.val)
65+
if (t.left != null) d[ta++] = t.left
66+
if (t.right != null) d[ta++] = t.right
67+
}
68+
return ans
69+
};
70+
```
71+
* 时间复杂度:$O(n)$
72+
* 空间复杂度:$O(n)$
73+
74+
---
75+
76+
### 递归 - DFS
77+
78+
使用「递归」来进行「层序遍历」虽然不太符合直观印象,但也是可以的。
79+
80+
此时我们需要借助「哈希表」来存储起来每一层的节点情况。
81+
82+
首先我们按照「中序遍历」的方式进行 `DFS`,同时在 `DFS` 过程中传递节点所在的深度(`root` 节点默认在深度最小的第 $0$ 层),每次处理当前节点时,通过哈希表获取所在层的数组,并将当前节点值追加到数组尾部,同时维护一个最大深度 `max`,在 `DFS` 完成后,再使用深度范围 $[0, max]$ 从哈希表中进行构造答案。
83+
84+
Java 代码:
85+
```Java
86+
class Solution {
87+
Map<Integer, List<Integer>> map = new HashMap<>();
88+
int max = -1, cnt = 0;
89+
public int[] levelOrder(TreeNode root) {
90+
dfs(root, 0);
91+
int[] ans = new int[cnt];
92+
for (int i = 0, idx = 0; i <= max; i++) {
93+
for (int x : map.get(i)) ans[idx++] = x;
94+
}
95+
return ans;
96+
}
97+
void dfs(TreeNode root, int depth) {
98+
if (root == null) return ;
99+
max = Math.max(max, depth);
100+
cnt++;
101+
dfs(root.left, depth + 1);
102+
List<Integer> list = map.getOrDefault(depth, new ArrayList<Integer>());
103+
list.add(root.val);
104+
map.put(depth, list);
105+
dfs(root.right, depth + 1);
106+
}
107+
}
108+
```
109+
TypeScript 代码:
110+
```TypeScript
111+
const map:Map<number, Array<number>> = new Map<number, Array<number>>()
112+
let max = -1, cnt = 0
113+
function levelOrder(root: TreeNode | null): number[] {
114+
map.clear()
115+
max = -1; cnt = 0;
116+
dfs(root, 0)
117+
const ans: Array<number> = new Array<number>()
118+
for (let i = 0; i <= max; i++) {
119+
for (const x of map.get(i)) ans.push(x)
120+
}
121+
return ans
122+
};
123+
function dfs(root: TreeNode | null, depth: number): void {
124+
if (root == null) return
125+
max = Math.max(max, depth)
126+
cnt++
127+
dfs(root.left, depth + 1)
128+
if (!map.has(depth)) map.set(depth, new Array<number>())
129+
map.get(depth).push(root.val)
130+
dfs(root.right, depth + 1)
131+
}
132+
```
133+
* 时间复杂度:$O(n)$
134+
* 空间复杂度:$O(n)$
135+
136+
---
137+
138+
### 最后
139+
140+
这是我们「刷穿 LeetCode」系列文章的第 `No.剑指 Offer 32 - I` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
141+
142+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
143+
144+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
145+
146+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
147+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/solution/by-ac_oier-s9jz/)** ,难度为 **简单**
4+
5+
Tag : 「二叉树」、「DFS」、「BFS」、「递归」、「迭代」
6+
7+
8+
9+
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
10+
11+
例如:
12+
给定二叉树: `[3,9,20,null,null,15,7]`,
13+
```
14+
3
15+
/ \
16+
9 20
17+
/ \
18+
15 7
19+
```
20+
返回其层次遍历结果:
21+
```
22+
[
23+
[3],
24+
[9,20],
25+
[15,7]
26+
]
27+
```
28+
29+
提示:
30+
* `节点总数 <= 1000`
31+
32+
---
33+
34+
### 迭代 - BFS
35+
36+
这道题是 [(题解)剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/solution/by-ac_oier-a9n5/) 的练习版本。
37+
38+
只需要在每次 `BFS` 拓展时,将完整的层进行取出,存如独立数组后再加入答案。
39+
40+
Java 代码:
41+
```Java
42+
class Solution {
43+
public List<List<Integer>> levelOrder(TreeNode root) {
44+
Deque<TreeNode> d = new ArrayDeque<>();
45+
if (root != null) d.addLast(root);
46+
List<List<Integer>> ans = new ArrayList<>();
47+
while (!d.isEmpty()) {
48+
int sz = d.size();
49+
List<Integer> list = new ArrayList<>();
50+
while (sz-- > 0) {
51+
TreeNode t = d.pollFirst();
52+
list.add(t.val);
53+
if (t.left != null) d.addLast(t.left);
54+
if (t.right != null) d.addLast(t.right);
55+
}
56+
ans.add(list);
57+
}
58+
return ans;
59+
}
60+
}
61+
```
62+
Typescript 代码:
63+
```Typescript
64+
function levelOrder(root: TreeNode | null): number[][] {
65+
const ans: number[][] = new Array<Array<number>>()
66+
const stk: TreeNode[] = new Array<TreeNode>()
67+
let he = 0, ta = 0
68+
if (root != null) stk[ta++] = root
69+
while (he < ta) {
70+
const temp = new Array<number>()
71+
let sz = ta - he
72+
while (sz-- > 0) {
73+
const t = stk[he++]
74+
temp.push(t.val)
75+
if (t.left != null) stk[ta++] = t.left
76+
if (t.right != null) stk[ta++] = t.right
77+
}
78+
ans.push(temp)
79+
}
80+
return ans
81+
};
82+
```
83+
* 时间复杂度:$O(n)$
84+
* 空间复杂度:$O(n)$
85+
86+
---
87+
88+
### 递归 - DFS
89+
90+
同理,可以使用「递归」来进行「层序遍历」。
91+
92+
此时我们需要借助「哈希表」来存储起来每一层的节点情况。
93+
94+
首先我们按照「中序遍历」的方式进行 `DFS`,同时在 `DFS` 过程中传递节点所在的深度(`root` 节点默认在深度最小的第 $0$ 层),每次处理当前节点时,通过哈希表获取所在层的数组,并将当前节点值追加到数组尾部,同时维护一个最大深度 `max`,在 `DFS` 完成后,再使用深度范围 $[0, max]$ 从哈希表中进行构造答案。
95+
96+
Java 代码:
97+
```Java
98+
class Solution {
99+
Map<Integer, List<Integer>> map = new HashMap<>();
100+
int max = -1;
101+
public List<List<Integer>> levelOrder(TreeNode root) {
102+
dfs(root, 0);
103+
List<List<Integer>> ans = new ArrayList<>();
104+
for (int i = 0; i <= max; i++) ans.add(map.get(i));
105+
return ans;
106+
}
107+
void dfs(TreeNode root, int depth) {
108+
if (root == null) return ;
109+
max = Math.max(max, depth);
110+
dfs(root.left, depth + 1);
111+
List<Integer> list = map.getOrDefault(depth, new ArrayList<>());
112+
list.add(root.val);
113+
map.put(depth, list);
114+
dfs(root.right, depth + 1);
115+
}
116+
}
117+
```
118+
TypeScript 代码:
119+
```TypeScript
120+
const map: Map<number, Array<number>> = new Map<number, Array<number>>()
121+
let max = -1
122+
function levelOrder(root: TreeNode | null): number[][] {
123+
map.clear()
124+
max = -1
125+
dfs(root, 0)
126+
const ans = new Array<Array<number>>()
127+
for (let i = 0; i <= max; i++) ans.push(map.get(i))
128+
return ans
129+
};
130+
function dfs(root: TreeNode | null, depth: number): void {
131+
if (root == null) return
132+
max = Math.max(max, depth)
133+
dfs(root.left, depth + 1)
134+
if (!map.has(depth)) map.set(depth, new Array<number>())
135+
map.get(depth).push(root.val)
136+
dfs(root.right, depth + 1)
137+
}
138+
```
139+
* 时间复杂度:$O(n)$
140+
* 空间复杂度:$O(n)$
141+
142+
---
143+
144+
### 最后
145+
146+
这是我们「刷穿 LeetCode」系列文章的第 `No.剑指 Offer 32 - II` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
147+
148+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
149+
150+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
151+
152+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
153+

0 commit comments

Comments
 (0)