mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
java: add «145. Binary Tree Postorder Traversal»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
765b40ad71
commit
8ab17a79b2
2 changed files with 37 additions and 0 deletions
17
java/TreeNode.java
Normal file
17
java/TreeNode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
20
java/binary-tree-postorder-traversal.java
Normal file
20
java/binary-tree-postorder-traversal.java
Normal 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>());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue