-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm145.java
More file actions
28 lines (22 loc) · 738 Bytes
/
prblm145.java
File metadata and controls
28 lines (22 loc) · 738 Bytes
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
import java.util.*;
public class prblm145 {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = null;
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
root.right.right = null;
System.out.println(new prblm145().postorderTraversal(root));
}
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
postOrder(root, ans);
return ans;
}
private void postOrder(TreeNode root, List<Integer> ans){
if(root == null) return;
postOrder(root.left, ans);
postOrder(root.right, ans);
ans.add(root.val);
}
}