-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution47.java
46 lines (44 loc) · 1.07 KB
/
Solution47.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.Stack;
/*
* Binary Tree Postorder Traversal
*/
public class Solution47 {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (root != null) {
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<TreeNode> temp = new Stack<TreeNode>();
temp.push(root);
while (!temp.isEmpty()) {
TreeNode node = temp.pop();
stack.push(node);
if (node.left != null)
temp.push(node.left);
if (node.right != null)
temp.push(node.right);
}
while (!stack.isEmpty())
result.add(stack.pop().val);
}
return result;
}
public static void main(String ...args) {
Solution47 s47 = new Solution47();
TreeNode root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
ArrayList<Integer> result = s47.postorderTraversal(root);
for (int i : result)
System.out.print(i+" ");
System.out.println();
}
}