File tree 3 files changed +162
-0
lines changed
solution/0400-0499/0429.N-ary Tree Level Order Traversal
3 files changed +162
-0
lines changed Original file line number Diff line number Diff line change @@ -227,6 +227,72 @@ func levelOrder(root *Node) [][]int {
227
227
}
228
228
```
229
229
230
+ ### ** TypeScript**
231
+
232
+ ``` ts
233
+ /**
234
+ * Definition for node.
235
+ * class Node {
236
+ * val: number
237
+ * children: Node[]
238
+ * constructor(val?: number) {
239
+ * this.val = (val===undefined ? 0 : val)
240
+ * this.children = []
241
+ * }
242
+ * }
243
+ */
244
+
245
+ function levelOrder(root : Node | null ): number [][] {
246
+ const res = [];
247
+ if (root == null ) {
248
+ return res ;
249
+ }
250
+ const queue = [root ];
251
+ while (queue .length !== 0 ) {
252
+ const n = queue .length ;
253
+ const vals = [];
254
+ for (let i = 0 ; i < n ; i ++ ) {
255
+ const { val, children } = queue .shift ();
256
+ vals .push (val );
257
+ queue .push (... children );
258
+ }
259
+ res .push (vals );
260
+ }
261
+ return res ;
262
+ }
263
+ ```
264
+
265
+ ``` ts
266
+ /**
267
+ * Definition for node.
268
+ * class Node {
269
+ * val: number
270
+ * children: Node[]
271
+ * constructor(val?: number) {
272
+ * this.val = (val===undefined ? 0 : val)
273
+ * this.children = []
274
+ * }
275
+ * }
276
+ */
277
+
278
+ function levelOrder(root : Node | null ): number [][] {
279
+ const res = [];
280
+ const dfs = (root : Node | null , depth : number ) => {
281
+ if (root == null ) {
282
+ return ;
283
+ }
284
+ if (res .length <= depth ) {
285
+ res .push ([]);
286
+ }
287
+ const { val, children } = root ;
288
+ res [depth ].push (val );
289
+ children .forEach (node => dfs (node , depth + 1 ));
290
+ };
291
+ dfs (root , 0 );
292
+ return res ;
293
+ }
294
+ ```
295
+
230
296
### ** ...**
231
297
232
298
```
Original file line number Diff line number Diff line change @@ -192,6 +192,72 @@ func levelOrder(root *Node) [][]int {
192
192
}
193
193
```
194
194
195
+ ### ** TypeScript**
196
+
197
+ ``` ts
198
+ /**
199
+ * Definition for node.
200
+ * class Node {
201
+ * val: number
202
+ * children: Node[]
203
+ * constructor(val?: number) {
204
+ * this.val = (val===undefined ? 0 : val)
205
+ * this.children = []
206
+ * }
207
+ * }
208
+ */
209
+
210
+ function levelOrder(root : Node | null ): number [][] {
211
+ const res = [];
212
+ if (root == null ) {
213
+ return res ;
214
+ }
215
+ const queue = [root ];
216
+ while (queue .length !== 0 ) {
217
+ const n = queue .length ;
218
+ const vals = [];
219
+ for (let i = 0 ; i < n ; i ++ ) {
220
+ const { val, children } = queue .shift ();
221
+ vals .push (val );
222
+ queue .push (... children );
223
+ }
224
+ res .push (vals );
225
+ }
226
+ return res ;
227
+ }
228
+ ```
229
+
230
+ ``` ts
231
+ /**
232
+ * Definition for node.
233
+ * class Node {
234
+ * val: number
235
+ * children: Node[]
236
+ * constructor(val?: number) {
237
+ * this.val = (val===undefined ? 0 : val)
238
+ * this.children = []
239
+ * }
240
+ * }
241
+ */
242
+
243
+ function levelOrder(root : Node | null ): number [][] {
244
+ const res = [];
245
+ const dfs = (root : Node | null , depth : number ) => {
246
+ if (root == null ) {
247
+ return ;
248
+ }
249
+ if (res .length <= depth ) {
250
+ res .push ([]);
251
+ }
252
+ const { val, children } = root ;
253
+ res [depth ].push (val );
254
+ children .forEach (node => dfs (node , depth + 1 ));
255
+ };
256
+ dfs (root , 0 );
257
+ return res ;
258
+ }
259
+ ```
260
+
195
261
### ** ...**
196
262
197
263
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for node.
3
+ * class Node {
4
+ * val: number
5
+ * children: Node[]
6
+ * constructor(val?: number) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.children = []
9
+ * }
10
+ * }
11
+ */
12
+
13
+ function levelOrder ( root : Node | null ) : number [ ] [ ] {
14
+ const res = [ ] ;
15
+ if ( root == null ) {
16
+ return res ;
17
+ }
18
+ const queue = [ root ] ;
19
+ while ( queue . length !== 0 ) {
20
+ const n = queue . length ;
21
+ const vals = [ ] ;
22
+ for ( let i = 0 ; i < n ; i ++ ) {
23
+ const { val, children } = queue . shift ( ) ;
24
+ vals . push ( val ) ;
25
+ queue . push ( ...children ) ;
26
+ }
27
+ res . push ( vals ) ;
28
+ }
29
+ return res ;
30
+ }
You can’t perform that action at this time.
0 commit comments