java: add «145. Binary Tree Postorder Traversal»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-25 14:11:31 +02:00
parent 765b40ad71
commit 8ab17a79b2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8
2 changed files with 37 additions and 0 deletions

17
java/TreeNode.java Normal file
View file

@ -0,0 +1,17 @@
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}

View file

@ -0,0 +1,20 @@
import java.util.ArrayList;
import java.util.List;
class Solution {
private ArrayList<Integer> postorderTraversal(TreeNode node, ArrayList<Integer> l) {
if (node == null) {
return l;
}
postorderTraversal(node.left, l);
postorderTraversal(node.right, l);
l.add(node.val);
return l;
}
public List<Integer> postorderTraversal(TreeNode root) {
return postorderTraversal(root, new ArrayList<Integer>());
}
}