-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreePostorderTraversal_145.java
77 lines (61 loc) · 2.08 KB
/
BinaryTreePostorderTraversal_145.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* ----------------------------------------------------------------------------
Binary Tree Postorder Traversal
- Given a binary tree, return the postorder traversal of its nodes' values.
For example:
- Given binary tree {1,#,2,3},
1
\
2
/
3
- return [3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
* ----------------------------------------------------------------------------
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/**
* 1. stack
* - all trees are pushed into the stack instead of just the left tree
* - need to check whether coming back from the left tree or right tree
* - to solve this, we maintain a lastvisit node to check whether
* the right tree has already visited
*/
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> postorder = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root, lastvisit = null;
while (curr !=null || !stack.isEmpty()) {
while (curr != null) {
stack.push(curr);
if (curr.left != null) // left tree
curr = curr.left;
else // right tree
curr = curr.right;
}
curr = stack.pop();
// two possibility here:
// 1) back from left tree; 2) back from right tree;
// 2) back from right tree
if (curr.right == null || curr.right == lastvisit) {
postorder.add(new Integer(curr.val));
lastvisit = curr;
curr = null; // $$$$ back from the entire tree, go on pop;
}
else { // 1) back from left tree
stack.push(curr); // push back second time
curr = curr.right; // work on the right subtree
}
}
return postorder;
}
}