-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_590.java
More file actions
32 lines (28 loc) · 788 Bytes
/
Copy pathP_590.java
File metadata and controls
32 lines (28 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package leetcode.easy;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
public class P_590 {
private static class Node {
public int val;
public List<Node> children;
}
public List<Integer> postorder(Node root) {
final List<Integer> res = new ArrayList<>();
final Deque<Node> stack = new ArrayDeque<>();
if (root != null) {
stack.addFirst(root);
}
while (!stack.isEmpty()) {
final Node curr = stack.removeFirst();
res.add(curr.val);
for (Node n : curr.children) {
stack.addFirst(n);
}
}
Collections.reverse(res);
return res;
}
}