1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

java: add «590. N-ary Tree Postorder Traversal»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-26 11:31:42 +02:00
parent 955f89bae0
commit 601c8144e2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8
2 changed files with 39 additions and 0 deletions

18
java/Node.java Normal file
View 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;
}
}
;

View 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>());
}
}