/
PostOrderNAryTraversal.java
43 lines (36 loc) · 1.06 KB
/
PostOrderNAryTraversal.java
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
33
34
35
36
37
38
39
40
41
42
43
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
// https://leetcode.com/problems/n-ary-tree-postorder-traversal/
class Solution {
// Either we can do a post order traversal in separate function and
// accumulate the results.
// Or we can do it within the same function.
// If we are going to do the later,
// we return empty list for null roots.
// we add the list result from traversing the children first and then add the current node value
public List<Integer> postorder(Node root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
for (Node n: root.children) {
List<Integer> fromChild = postorder(n);
res.addAll(fromChild);
}
res.add(root.val);
return res;
}
}