Skip to content

Commit 153ebae

Browse files
add 590
1 parent 1797e75 commit 153ebae

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Your ideas/fixes/algorithms are more than welcome!
191191
|593|[Valid Square](https://leetcode.com/problems/valid-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_593.java) | O(1) |O(1) | |Medium | Math
192192
|592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_592.java) | O(nlogx) |O(n) | |Medium | Math
193193
|591|[Tag Validator](https://leetcode.com/problems/tag-validator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_591.java) | O(n) |O(n) | |Hard | Stack, String
194+
|590|[N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_590.java) | O(n) |O(n) | |Easy| DFS, recursion
194195
|589|[N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_589.java) | O(n) |O(n) | |Easy | DFS, recursion
195196
|588|[Design In-Memory File System](https://leetcode.com/problems/design-in-memory-file-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_588.java) | O(n) |O(h) | |Hard | Trie, Design
196197
|587|[Erect the Fence](https://leetcode.com/problems/erect-the-fence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_587.java) | O(?) |O(?) | |Hard | Geometry
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.Node;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* 590. N-ary Tree Postorder Traversal
9+
*
10+
* Given an n-ary tree, return the postorder traversal of its nodes' values.
11+
*
12+
* For example, given a 3-ary tree:
13+
*
14+
* 1
15+
* / | \
16+
* 3 2 4
17+
* / \
18+
* 5 6
19+
*
20+
* Return its postorder traversal as: [5,6,3,2,4,1].
21+
*
22+
* Note:
23+
*
24+
* Recursive solution is trivial, could you do it iteratively?
25+
*/
26+
public class _590 {
27+
public static class Solution1 {
28+
public List<Integer> postorder(Node root) {
29+
List<Integer> result = new ArrayList<>();
30+
if (root == null) {
31+
return result;
32+
}
33+
dfs(root, result);
34+
result.add(root.val);
35+
return result;
36+
}
37+
38+
private void dfs(Node root, List<Integer> result) {
39+
if (root == null) {
40+
return;
41+
}
42+
if (root.children.size() > 0) {
43+
for (Node child : root.children) {
44+
dfs(child, result);
45+
result.add(child.val);
46+
}
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)