43
43
44
44
### 方法一:BFS
45
45
46
- 借助队列,逐层遍历 。
46
+ 我们首先判断根节点是否为空,若为空则直接返回空列表 。
47
47
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 叉树的节点数。
49
59
50
60
<!-- tabs:start -->
51
61
@@ -142,15 +152,19 @@ class Solution {
142
152
public:
143
153
vector<vector<int >> levelOrder(Node* root) {
144
154
vector<vector<int >> ans;
145
- if (!root) return ans;
155
+ if (!root) {
156
+ return ans;
157
+ }
146
158
queue<Node* > q{{root}};
147
159
while (!q.empty()) {
148
160
vector<int > t;
149
- for (int n = q.size(); n > 0 ; --n) {
161
+ for (int n = q.size(); n; --n) {
150
162
root = q.front();
151
163
q.pop();
152
164
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
+ }
154
168
}
155
169
ans.push_back(t);
156
170
}
@@ -168,10 +182,9 @@ public:
168
182
* }
169
183
*/
170
184
171
- func levelOrder(root *Node) [][]int {
172
- var ans [][]int
185
+ func levelOrder(root *Node) (ans [][]int) {
173
186
if root == nil {
174
- return ans
187
+ return
175
188
}
176
189
q := []*Node{root}
177
190
for len(q) > 0 {
@@ -186,7 +199,7 @@ func levelOrder(root *Node) [][]int {
186
199
}
187
200
ans = append(ans, t)
188
201
}
189
- return ans
202
+ return
190
203
}
191
204
```
192
205
@@ -204,34 +217,42 @@ func levelOrder(root *Node) [][]int {
204
217
*/
205
218
206
219
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 ;
210
223
}
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 );
219
231
}
220
- res .push (vals );
232
+ ans .push (t );
233
+ q .splice (0 , q .length , ... qq );
221
234
}
222
- return res ;
235
+ return ans ;
223
236
}
224
237
```
225
238
226
239
<!-- tabs: end -->
227
240
228
241
### 方法二:DFS
229
242
230
- 按深度遍历。
243
+ 我们可以使用深度优先搜索的方法,遍历整棵树。
244
+
245
+ 我们定义一个辅助函数 $dfs(root, i)$,其中 $root$ 表示当前节点,而 $i$ 表示当前层数。
246
+
247
+ 在 $dfs$ 函数中,我们首先判断 $root$ 是否为空,若为空则直接返回。
231
248
232
- 假设当前深度为 i,遍历到的节点为 root。若结果列表 ` ans[i] ` 不存在,则创建一个空列表放入 ans 中,然后将 ` root.val ` 放入 ` ans[i] ` 。接着往下一层遍历(root 的子节点) 。
249
+ 否则,我们判断 $ ans$ 的长度是否小于等于 $i$,若是则说明当前层还没有加入到 $ ans$ 中,我们需要先加入一个空列表。 然后将 $ root$ 的值加入 $ ans[ i] $ 中 。
233
250
234
- 时间复杂度 $O(n)$。
251
+ 接着,我们遍历 $root$ 的所有子节点,对于每个子节点,我们调用 $dfs(child, i + 1)$。
252
+
253
+ 在主函数中,我们调用 $dfs(root, 0)$,并返回 $ans$。
254
+
255
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 N 叉树的节点数。
235
256
236
257
<!-- tabs: start -->
237
258
@@ -282,13 +303,14 @@ class Node {
282
303
*/
283
304
284
305
class Solution {
306
+ private List<List<Integer > > ans = new ArrayList<> ();
307
+
285
308
public List<List<Integer > > levelOrder (Node root ) {
286
- List<List<Integer > > ans = new ArrayList<> ();
287
- dfs(root, 0 , ans);
309
+ dfs(root, 0 );
288
310
return ans;
289
311
}
290
312
291
- private void dfs (Node root , int i , List< List< Integer > > ans ) {
313
+ private void dfs (Node root , int i ) {
292
314
if (root == null ) {
293
315
return ;
294
316
}
@@ -297,7 +319,7 @@ class Solution {
297
319
}
298
320
ans. get(i++ ). add(root. val);
299
321
for (Node child : root. children) {
300
- dfs(child, i, ans );
322
+ dfs(child, i);
301
323
}
302
324
}
303
325
}
@@ -328,16 +350,21 @@ class Solution {
328
350
public:
329
351
vector<vector<int >> levelOrder(Node* root) {
330
352
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);
332
366
return ans;
333
367
}
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
- }
341
368
};
342
369
```
343
370
@@ -350,8 +377,7 @@ public:
350
377
* }
351
378
*/
352
379
353
- func levelOrder (root *Node ) [][]int {
354
- var ans [][]int
380
+ func levelOrder(root *Node) (ans [][]int) {
355
381
var dfs func(root *Node, i int)
356
382
dfs = func(root *Node, i int) {
357
383
if root == nil {
@@ -366,7 +392,7 @@ func levelOrder(root *Node) [][]int {
366
392
}
367
393
}
368
394
dfs(root, 0)
369
- return ans
395
+ return
370
396
}
371
397
```
372
398
@@ -384,20 +410,20 @@ func levelOrder(root *Node) [][]int {
384
410
*/
385
411
386
412
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 ) {
390
416
return ;
391
417
}
392
- if (res .length <= depth ) {
393
- res .push ([]);
418
+ if (ans .length <= i ) {
419
+ ans .push ([]);
394
420
}
395
421
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 ));
398
424
};
399
425
dfs (root , 0 );
400
- return res ;
426
+ return ans ;
401
427
}
402
428
```
403
429
0 commit comments