Skip to content

Commit 6f0b794

Browse files
authored
feat: add solutions to lc problem: No.0429 (doocs#2346)
No.0429.N-ary Tree Level Order Traversal
1 parent 616ea7b commit 6f0b794

File tree

9 files changed

+206
-136
lines changed

9 files changed

+206
-136
lines changed

solution/0400-0499/0429.N-ary Tree Level Order Traversal/README.md

+74-48
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@
4343

4444
### 方法一:BFS
4545

46-
借助队列,逐层遍历
46+
我们首先判断根节点是否为空,若为空则直接返回空列表
4747

48-
时间复杂度 $O(n)$。
48+
否则,我们创建一个队列 $q$,初始时将根节点加入队列。
49+
50+
当队列不为空时,我们循环以下操作:
51+
52+
1. 创建一个空列表 $t$,用于存放当前层的节点值。
53+
2. 对于队列中的每个节点,将其值加入 $t$ 中,并将其子节点加入队列。
54+
3. 将 $t$ 加入结果列表 $ans$。
55+
56+
最后返回结果列表 $ans$。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 N 叉树的节点数。
4959

5060
<!-- tabs:start -->
5161

@@ -142,15 +152,19 @@ class Solution {
142152
public:
143153
vector<vector<int>> levelOrder(Node* root) {
144154
vector<vector<int>> ans;
145-
if (!root) return ans;
155+
if (!root) {
156+
return ans;
157+
}
146158
queue<Node*> q{{root}};
147159
while (!q.empty()) {
148160
vector<int> t;
149-
for (int n = q.size(); n > 0; --n) {
161+
for (int n = q.size(); n; --n) {
150162
root = q.front();
151163
q.pop();
152164
t.push_back(root->val);
153-
for (auto& child : root->children) q.push(child);
165+
for (auto& child : root->children) {
166+
q.push(child);
167+
}
154168
}
155169
ans.push_back(t);
156170
}
@@ -168,10 +182,9 @@ public:
168182
* }
169183
*/
170184
171-
func levelOrder(root *Node) [][]int {
172-
var ans [][]int
185+
func levelOrder(root *Node) (ans [][]int) {
173186
if root == nil {
174-
return ans
187+
return
175188
}
176189
q := []*Node{root}
177190
for len(q) > 0 {
@@ -186,7 +199,7 @@ func levelOrder(root *Node) [][]int {
186199
}
187200
ans = append(ans, t)
188201
}
189-
return ans
202+
return
190203
}
191204
```
192205

@@ -204,34 +217,42 @@ func levelOrder(root *Node) [][]int {
204217
*/
205218

206219
function levelOrder(root: Node | null): number[][] {
207-
const res = [];
208-
if (root == null) {
209-
return res;
220+
const ans: number[][] = [];
221+
if (!root) {
222+
return ans;
210223
}
211-
const queue = [root];
212-
while (queue.length !== 0) {
213-
const n = queue.length;
214-
const vals = [];
215-
for (let i = 0; i < n; i++) {
216-
const { val, children } = queue.shift();
217-
vals.push(val);
218-
queue.push(...children);
224+
const q: Node[] = [root];
225+
while (q.length) {
226+
const qq: Node[] = [];
227+
const t: number[] = [];
228+
for (const { val, children } of q) {
229+
qq.push(...children);
230+
t.push(val);
219231
}
220-
res.push(vals);
232+
ans.push(t);
233+
q.splice(0, q.length, ...qq);
221234
}
222-
return res;
235+
return ans;
223236
}
224237
```
225238

226239
<!-- tabs:end -->
227240

228241
### 方法二:DFS
229242

230-
按深度遍历。
243+
我们可以使用深度优先搜索的方法,遍历整棵树。
244+
245+
我们定义一个辅助函数 $dfs(root, i)$,其中 $root$ 表示当前节点,而 $i$ 表示当前层数。
246+
247+
在 $dfs$ 函数中,我们首先判断 $root$ 是否为空,若为空则直接返回。
231248

232-
假设当前深度为 i,遍历到的节点为 root。若结果列表 `ans[i]` 不存在,则创建一个空列表放入 ans 中,然后将 `root.val` 放入 `ans[i]`。接着往下一层遍历(root 的子节点)
249+
否则,我们判断 $ans$ 的长度是否小于等于 $i$,若是则说明当前层还没有加入到 $ans$ 中,我们需要先加入一个空列表。然后将 $root$ 的值加入 $ans[i]$ 中
233250

234-
时间复杂度 $O(n)$。
251+
接着,我们遍历 $root$ 的所有子节点,对于每个子节点,我们调用 $dfs(child, i + 1)$。
252+
253+
在主函数中,我们调用 $dfs(root, 0)$,并返回 $ans$。
254+
255+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 N 叉树的节点数。
235256

236257
<!-- tabs:start -->
237258

@@ -282,13 +303,14 @@ class Node {
282303
*/
283304

284305
class Solution {
306+
private List<List<Integer>> ans = new ArrayList<>();
307+
285308
public List<List<Integer>> levelOrder(Node root) {
286-
List<List<Integer>> ans = new ArrayList<>();
287-
dfs(root, 0, ans);
309+
dfs(root, 0);
288310
return ans;
289311
}
290312

291-
private void dfs(Node root, int i, List<List<Integer>> ans) {
313+
private void dfs(Node root, int i) {
292314
if (root == null) {
293315
return;
294316
}
@@ -297,7 +319,7 @@ class Solution {
297319
}
298320
ans.get(i++).add(root.val);
299321
for (Node child : root.children) {
300-
dfs(child, i, ans);
322+
dfs(child, i);
301323
}
302324
}
303325
}
@@ -328,16 +350,21 @@ class Solution {
328350
public:
329351
vector<vector<int>> levelOrder(Node* root) {
330352
vector<vector<int>> ans;
331-
dfs(root, 0, ans);
353+
function<void(Node*, int i)> dfs = [&](Node* root, int i) {
354+
if (!root) {
355+
return;
356+
}
357+
if (ans.size() <= i) {
358+
ans.push_back({});
359+
}
360+
ans[i++].push_back(root->val);
361+
for (auto& child : root->children) {
362+
dfs(child, i);
363+
}
364+
};
365+
dfs(root, 0);
332366
return ans;
333367
}
334-
335-
void dfs(Node* root, int i, vector<vector<int>>& ans) {
336-
if (!root) return;
337-
if (ans.size() <= i) ans.push_back({});
338-
ans[i++].push_back(root->val);
339-
for (Node* child : root->children) dfs(child, i, ans);
340-
}
341368
};
342369
```
343370
@@ -350,8 +377,7 @@ public:
350377
* }
351378
*/
352379
353-
func levelOrder(root *Node) [][]int {
354-
var ans [][]int
380+
func levelOrder(root *Node) (ans [][]int) {
355381
var dfs func(root *Node, i int)
356382
dfs = func(root *Node, i int) {
357383
if root == nil {
@@ -366,7 +392,7 @@ func levelOrder(root *Node) [][]int {
366392
}
367393
}
368394
dfs(root, 0)
369-
return ans
395+
return
370396
}
371397
```
372398

@@ -384,20 +410,20 @@ func levelOrder(root *Node) [][]int {
384410
*/
385411

386412
function levelOrder(root: Node | null): number[][] {
387-
const res = [];
388-
const dfs = (root: Node | null, depth: number) => {
389-
if (root == null) {
413+
const ans: number[][] = [];
414+
const dfs = (root: Node | null, i: number) => {
415+
if (root === null) {
390416
return;
391417
}
392-
if (res.length <= depth) {
393-
res.push([]);
418+
if (ans.length <= i) {
419+
ans.push([]);
394420
}
395421
const { val, children } = root;
396-
res[depth].push(val);
397-
children.forEach(node => dfs(node, depth + 1));
422+
ans[i++].push(val);
423+
children.forEach(node => dfs(node, i));
398424
};
399425
dfs(root, 0);
400-
return res;
426+
return ans;
401427
}
402428
```
403429

0 commit comments

Comments
 (0)