Skip to content

Commit daba4fe

Browse files
committedMar 12, 2022
feat: add ts solution to lc problems: No.0589, 590
- No.0589.N-ary Tree Preorder Traversal - No.0590.N-ary Tree Postorder Traversal
1 parent 05c5b19 commit daba4fe

File tree

6 files changed

+300
-0
lines changed

6 files changed

+300
-0
lines changed
 

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

+58
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,64 @@ func preorder(root *Node) []int {
195195
}
196196
```
197197

198+
### **TypeScript**
199+
200+
```ts
201+
/**
202+
* Definition for node.
203+
* class Node {
204+
* val: number
205+
* children: Node[]
206+
* constructor(val?: number) {
207+
* this.val = (val===undefined ? 0 : val)
208+
* this.children = []
209+
* }
210+
* }
211+
*/
212+
213+
function preorder(root: Node | null): number[] {
214+
const res = [];
215+
const stack = [root];
216+
while (stack.length !== 0) {
217+
const root = stack.pop();
218+
if (root != null) {
219+
res.push(root.val);
220+
stack.push(...root.children.reverse());
221+
}
222+
}
223+
return res;
224+
}
225+
```
226+
227+
```ts
228+
/**
229+
* Definition for node.
230+
* class Node {
231+
* val: number
232+
* children: Node[]
233+
* constructor(val?: number) {
234+
* this.val = (val===undefined ? 0 : val)
235+
* this.children = []
236+
* }
237+
* }
238+
*/
239+
240+
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;
253+
}
254+
```
255+
198256
### **...**
199257

200258
```

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

+58
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,64 @@ func preorder(root *Node) []int {
185185
}
186186
```
187187

188+
### **TypeScript**
189+
190+
```ts
191+
/**
192+
* Definition for node.
193+
* class Node {
194+
* val: number
195+
* children: Node[]
196+
* constructor(val?: number) {
197+
* this.val = (val===undefined ? 0 : val)
198+
* this.children = []
199+
* }
200+
* }
201+
*/
202+
203+
function preorder(root: Node | null): number[] {
204+
const res = [];
205+
const stack = [root];
206+
while (stack.length !== 0) {
207+
const root = stack.pop();
208+
if (root != null) {
209+
res.push(root.val);
210+
stack.push(...root.children.reverse());
211+
}
212+
}
213+
return res;
214+
}
215+
```
216+
217+
```ts
218+
/**
219+
* Definition for node.
220+
* class Node {
221+
* val: number
222+
* children: Node[]
223+
* constructor(val?: number) {
224+
* this.val = (val===undefined ? 0 : val)
225+
* this.children = []
226+
* }
227+
* }
228+
*/
229+
230+
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;
243+
}
244+
```
245+
188246
### **...**
189247

190248
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 preorder(root: Node | null): number[] {
14+
const res = [];
15+
const stack = [root];
16+
while (stack.length !== 0) {
17+
const root = stack.pop();
18+
if (root != null) {
19+
res.push(root.val);
20+
stack.push(...root.children.reverse());
21+
}
22+
}
23+
return res;
24+
}

‎solution/0500-0599/0590.N-ary Tree Postorder Traversal/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,75 @@ func postorder(root *Node) []int {
345345
}
346346
```
347347

348+
### **TypeScript**
349+
350+
递归:
351+
352+
```ts
353+
/**
354+
* Definition for node.
355+
* class Node {
356+
* val: number
357+
* children: Node[]
358+
* constructor(val?: number) {
359+
* this.val = (val===undefined ? 0 : val)
360+
* this.children = []
361+
* }
362+
* }
363+
*/
364+
365+
function postorder(root: Node | null): number[] {
366+
const res = [];
367+
const dfs = (root: Node | null) => {
368+
if (root == null) {
369+
return;
370+
}
371+
for (const node of root.children) {
372+
dfs(node);
373+
}
374+
res.push(root.val);
375+
};
376+
dfs(root);
377+
return res;
378+
}
379+
```
380+
381+
迭代:
382+
383+
```ts
384+
/**
385+
* Definition for node.
386+
* class Node {
387+
* val: number
388+
* children: Node[]
389+
* constructor(val?: number) {
390+
* this.val = (val===undefined ? 0 : val)
391+
* this.children = []
392+
* }
393+
* }
394+
*/
395+
396+
function postorder(root: Node | null): number[] {
397+
const res = [];
398+
if (root == null) {
399+
return res;
400+
}
401+
const stack = [root];
402+
while (stack.length !== 0) {
403+
const target = stack[stack.length - 1];
404+
if (target.children == null) {
405+
res.push(stack.pop().val);
406+
} else {
407+
for (let i = target.children.length - 1; i >= 0; i--) {
408+
stack.push(target.children[i]);
409+
}
410+
target.children = null;
411+
}
412+
}
413+
return res;
414+
}
415+
```
416+
348417
### **...**
349418

350419
```

‎solution/0500-0599/0590.N-ary Tree Postorder Traversal/README_EN.md

+65
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,71 @@ func postorder(root *Node) []int {
353353
}
354354
```
355355

356+
### **TypeScript**
357+
358+
```ts
359+
/**
360+
* Definition for node.
361+
* class Node {
362+
* val: number
363+
* children: Node[]
364+
* constructor(val?: number) {
365+
* this.val = (val===undefined ? 0 : val)
366+
* this.children = []
367+
* }
368+
* }
369+
*/
370+
371+
function postorder(root: Node | null): number[] {
372+
const res = [];
373+
const dfs = (root: Node | null) => {
374+
if (root == null) {
375+
return;
376+
}
377+
for (const node of root.children) {
378+
dfs(node);
379+
}
380+
res.push(root.val);
381+
};
382+
dfs(root);
383+
return res;
384+
}
385+
```
386+
387+
```ts
388+
/**
389+
* Definition for node.
390+
* class Node {
391+
* val: number
392+
* children: Node[]
393+
* constructor(val?: number) {
394+
* this.val = (val===undefined ? 0 : val)
395+
* this.children = []
396+
* }
397+
* }
398+
*/
399+
400+
function postorder(root: Node | null): number[] {
401+
const res = [];
402+
if (root == null) {
403+
return res;
404+
}
405+
const stack = [root];
406+
while (stack.length !== 0) {
407+
const target = stack[stack.length - 1];
408+
if (target.children == null) {
409+
res.push(stack.pop().val);
410+
} else {
411+
for (let i = target.children.length - 1; i >= 0; i--) {
412+
stack.push(target.children[i]);
413+
}
414+
target.children = null;
415+
}
416+
}
417+
return res;
418+
}
419+
```
420+
356421
### **...**
357422

358423
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 postorder(root: Node | null): number[] {
14+
const res = [];
15+
const dfs = (root: Node | null) => {
16+
if (root == null) {
17+
return;
18+
}
19+
for (const node of root.children) {
20+
dfs(node);
21+
}
22+
res.push(root.val);
23+
};
24+
dfs(root);
25+
return res;
26+
}

0 commit comments

Comments
 (0)