Skip to content

Commit 22de3eb

Browse files
committed
feat: update ts solution to lc problem: No.0589
No.0589.N-ary Tree Preorder Traversal
1 parent 442a599 commit 22de3eb

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

solution/0500-0599/0589.N-ary Tree Preorder Traversal/README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,16 @@ func preorder(root *Node) []int {
212212

213213
function preorder(root: Node | null): number[] {
214214
const res = [];
215+
if (root == null) {
216+
return res;
217+
}
215218
const stack = [root];
216219
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]);
221225
}
222226
}
223227
return res;
@@ -238,18 +242,16 @@ function preorder(root: Node | null): number[] {
238242
*/
239243

240244
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+
];
253255
}
254256
```
255257

solution/0500-0599/0589.N-ary Tree Preorder Traversal/README_EN.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,16 @@ func preorder(root *Node) []int {
202202

203203
function preorder(root: Node | null): number[] {
204204
const res = [];
205+
if (root == null) {
206+
return res;
207+
}
205208
const stack = [root];
206209
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]);
211215
}
212216
}
213217
return res;
@@ -228,18 +232,16 @@ function preorder(root: Node | null): number[] {
228232
*/
229233

230234
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+
];
243245
}
244246
```
245247

solution/0500-0599/0589.N-ary Tree Preorder Traversal/Solution.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212

1313
function preorder(root: Node | null): number[] {
1414
const res = [];
15+
if (root == null) {
16+
return res;
17+
}
1518
const stack = [root];
1619
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]);
2125
}
2226
}
2327
return res;

0 commit comments

Comments
 (0)