File tree Expand file tree Collapse file tree 3 files changed +44
-36
lines changed
solution/0500-0599/0589.N-ary Tree Preorder Traversal Expand file tree Collapse file tree 3 files changed +44
-36
lines changed Original file line number Diff line number Diff line change @@ -212,12 +212,16 @@ func preorder(root *Node) []int {
212
212
213
213
function preorder(root : Node | null ): number [] {
214
214
const res = [];
215
+ if (root == null ) {
216
+ return res ;
217
+ }
215
218
const stack = [root ];
216
219
while (stack .length !== 0 ) {
217
- const root = stack .pop ();
218
- if (root != null ) {
219
- res .push (root .val );
220
- stack .push (... root .children .reverse ());
220
+ const { val, children } = stack .pop ();
221
+ res .push (val );
222
+ const n = children .length ;
223
+ for (let i = n - 1 ; i >= 0 ; i -- ) {
224
+ stack .push (children [i ]);
221
225
}
222
226
}
223
227
return res ;
@@ -238,18 +242,16 @@ function preorder(root: Node | null): number[] {
238
242
*/
239
243
240
244
function preorder(root : Node | null ): number [] {
241
- const res = [];
242
- const dfs = (root : Node | null ) => {
243
- if (root == null ) {
244
- return ;
245
- }
246
- res .push (root .val );
247
- for (const node of root .children ) {
248
- dfs (node );
249
- }
250
- };
251
- dfs (root );
252
- return res ;
245
+ if (root == null ) {
246
+ return [];
247
+ }
248
+ const { val, children } = root ;
249
+ return [
250
+ val ,
251
+ ... children
252
+ .map (node => preorder (node ))
253
+ .reduce ((p , v ) => p .concat (v ), []),
254
+ ];
253
255
}
254
256
```
255
257
Original file line number Diff line number Diff line change @@ -202,12 +202,16 @@ func preorder(root *Node) []int {
202
202
203
203
function preorder(root : Node | null ): number [] {
204
204
const res = [];
205
+ if (root == null ) {
206
+ return res ;
207
+ }
205
208
const stack = [root ];
206
209
while (stack .length !== 0 ) {
207
- const root = stack .pop ();
208
- if (root != null ) {
209
- res .push (root .val );
210
- stack .push (... root .children .reverse ());
210
+ const { val, children } = stack .pop ();
211
+ res .push (val );
212
+ const n = children .length ;
213
+ for (let i = n - 1 ; i >= 0 ; i -- ) {
214
+ stack .push (children [i ]);
211
215
}
212
216
}
213
217
return res ;
@@ -228,18 +232,16 @@ function preorder(root: Node | null): number[] {
228
232
*/
229
233
230
234
function preorder(root : Node | null ): number [] {
231
- const res = [];
232
- const dfs = (root : Node | null ) => {
233
- if (root == null ) {
234
- return ;
235
- }
236
- res .push (root .val );
237
- for (const node of root .children ) {
238
- dfs (node );
239
- }
240
- };
241
- dfs (root );
242
- return res ;
235
+ if (root == null ) {
236
+ return [];
237
+ }
238
+ const { val, children } = root ;
239
+ return [
240
+ val ,
241
+ ... children
242
+ .map (node => preorder (node ))
243
+ .reduce ((p , v ) => p .concat (v ), []),
244
+ ];
243
245
}
244
246
```
245
247
Original file line number Diff line number Diff line change 12
12
13
13
function preorder ( root : Node | null ) : number [ ] {
14
14
const res = [ ] ;
15
+ if ( root == null ) {
16
+ return res ;
17
+ }
15
18
const stack = [ root ] ;
16
19
while ( stack . length !== 0 ) {
17
- const root = stack . pop ( ) ;
18
- if ( root != null ) {
19
- res . push ( root . val ) ;
20
- stack . push ( ...root . children . reverse ( ) ) ;
20
+ const { val, children } = stack . pop ( ) ;
21
+ res . push ( val ) ;
22
+ const n = children . length ;
23
+ for ( let i = n - 1 ; i >= 0 ; i -- ) {
24
+ stack . push ( children [ i ] ) ;
21
25
}
22
26
}
23
27
return res ;
You can’t perform that action at this time.
0 commit comments