Skip to content

Commit a7f0f77

Browse files
zhouboyi1998zhouboyi
authored andcommitted
docs(note): 添加题解:102. 二叉树的层序遍历
1 parent 53239d3 commit a7f0f77

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

markdown/hello/0000.Hello.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| <span style="color: #00AF9B;">**Easy**</span> | [**0094.二叉树的中序遍历**](../easy/0094.二叉树的中序遍历.md) |
3838
| <span style="color: #00AF9B;">**Easy**</span> | [**0100.相同的树**](../easy/0100.相同的树.md) |
3939
| <span style="color: #00AF9B;">**Easy**</span> | [**0101.对称二叉树**](../easy/0101.对称二叉树.md) |
40+
| <span style="color: #FFB822;">**Medium**</span> | [**0102.二叉树的层序遍历**](../medium/0102.二叉树的层序遍历.md) |
4041
| <span style="color: #00AF9B;">**Easy**</span> | [**0104.二叉树的最大深度**](../easy/0104.二叉树的最大深度.md) |
4142
| <span style="color: #00AF9B;">**Easy**</span> | [**0108.将有序数组转换为二叉搜索树**](../easy/0108.将有序数组转换为二叉搜索树.md) |
4243
| <span style="color: #00AF9B;">**Easy**</span> | [**0112.路径总和**](../easy/0112.路径总和.md) |
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<h1 style="text-align: center;"> <span style="color: #FFB822;">102. 二叉树的层序遍历</span> </h1>
2+
3+
### 🚀 LeetCode
4+
5+
<base target="_blank">
6+
7+
<span style="color: #FFB822;">**Medium**</span> [**https://leetcode.cn/problems/binary-tree-level-order-traversal/**](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
8+
9+
---
10+
11+
### ❓ Description
12+
13+
<br/>
14+
15+
给你二叉树的根节点 `root`,返回其节点值的 **层序遍历**。(即逐层地,从左到右访问所有节点)。
16+
17+
<br/>
18+
19+
**示例 1:**
20+
21+
<img src="../../public/0102/tree-1.jpg" alt="tree-1.jpg"/>
22+
23+
```
24+
输入:root = [3, 9, 20, null, null, 15, 7]
25+
输出:[[3], [9, 20], [15, 7]]
26+
```
27+
28+
**示例 2:**
29+
30+
```
31+
输入: root = [1]
32+
输出: [[1]]
33+
```
34+
35+
**示例 3:**
36+
37+
```
38+
输入: root = []
39+
输出: []
40+
```
41+
42+
<br/>
43+
44+
**提示:**
45+
46+
* 树中节点数目在范围 `[0, 2000]`
47+
* `-1000 <= Node.val <= 1000`
48+
49+
---
50+
51+
### ❗ Solution
52+
53+
<br/>
54+
55+
#### Java
56+
57+
```
58+
/**
59+
* Definition for a binary tree node.
60+
* public class TreeNode {
61+
* int val;
62+
* TreeNode left;
63+
* TreeNode right;
64+
* TreeNode() {}
65+
* TreeNode(int val) { this.val = val; }
66+
* TreeNode(int val, TreeNode left, TreeNode right) {
67+
* this.val = val;
68+
* this.left = left;
69+
* this.right = right;
70+
* }
71+
* }
72+
*/
73+
class Solution {
74+
public List<List<Integer>> levelOrder(TreeNode root) {
75+
if (root == null) {
76+
return new ArrayList<>();
77+
}
78+
79+
List<List<Integer>> result = new ArrayList<>();
80+
List<Integer> item = new ArrayList<>();
81+
82+
// 利用队列先进先出的特点, 存储下一层树节点
83+
// 当遍历完当前层的树节点, 将下一层的树节点依次取出进行操作
84+
Queue<TreeNode> queue = new LinkedList<>();
85+
queue.add(root);
86+
87+
// 当前层级的最大节点数
88+
int maxNum = 1;
89+
// 下一层级的最大节点数需要扣减的数量
90+
int subNum = 0;
91+
// 当前层级已添加的节点数
92+
int curNum = 0;
93+
94+
while (!queue.isEmpty()) {
95+
TreeNode node = queue.poll();
96+
if (node != null) {
97+
item.add(node.val);
98+
queue.add(node.left);
99+
queue.add(node.right);
100+
} else {
101+
// 当前层级每有 1 个节点为 null, 下一层级的最大节点数就需要 - 2
102+
subNum += 2;
103+
}
104+
// 当前层级已添加的节点数 + 1
105+
curNum++;
106+
// 如果当前层级已添加的节点数 = 当前层级的最大节点数, 切换到下一层级
107+
if (curNum == maxNum) {
108+
// 如果当前层级有节点, 添加到结果中
109+
if (item.size() > 0) {
110+
result.add(item);
111+
}
112+
// 重置各项标志
113+
item = new ArrayList<>();
114+
maxNum = maxNum * 2 - subNum;
115+
subNum = 0;
116+
curNum = 0;
117+
}
118+
}
119+
120+
return result;
121+
}
122+
}
123+
```

public/0102/tree-1.jpg

11.1 KB
Loading

0 commit comments

Comments
 (0)