java: add «590. N-ary Tree Postorder Traversal»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
955f89bae0
commit
601c8144e2
2 changed files with 39 additions and 0 deletions
18
java/Node.java
Normal file
18
java/Node.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
import java.util.List;
|
||||
|
||||
public 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;
|
||||
}
|
||||
}
|
||||
;
|
21
java/n-ary-tree-postorder-traversal.java
Normal file
21
java/n-ary-tree-postorder-traversal.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Solution {
|
||||
private ArrayList<Integer> postorder(Node node, ArrayList<Integer> l) {
|
||||
if (node == null) {
|
||||
return l;
|
||||
}
|
||||
|
||||
for (var child : node.children) {
|
||||
postorder(child, l);
|
||||
}
|
||||
l.add(node.val);
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
public List<Integer> postorder(Node root) {
|
||||
return postorder(root, new ArrayList<Integer>());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue