Skip to content

Commit 5fd697b

Browse files
refactor 590
1 parent 9461653 commit 5fd697b

File tree

1 file changed

+21
-39
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+21
-39
lines changed
Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.Node;
4+
45
import java.util.ArrayList;
56
import java.util.List;
67

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-
*/
268
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-
}
9+
public static class Solution1 {
10+
public List<Integer> postorder(Node root) {
11+
List<Integer> result = new ArrayList<>();
12+
if (root == null) {
13+
return result;
14+
}
15+
dfs(root, result);
16+
result.add(root.val);
17+
return result;
18+
}
3719

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);
20+
private void dfs(Node root, List<Integer> result) {
21+
if (root == null) {
22+
return;
23+
}
24+
if (root.children.size() > 0) {
25+
for (Node child : root.children) {
26+
dfs(child, result);
27+
result.add(child.val);
28+
}
29+
}
4630
}
47-
}
4831
}
49-
}
5032
}

0 commit comments

Comments
 (0)