forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreePostOrderTraversal.java
57 lines (49 loc) · 2.07 KB
/
BinaryTreePostOrderTraversal.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
package medium;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import utils.CommonUtils;
import classes.TreeNode;
public class BinaryTreePostOrderTraversal {
/**I was really confused about how iterative version works for POST-order traversal, since we'lld need to add a field in TreeNode
class called "visited", to mark this node has been visited before, otherwise it goes into indefinite loop.
Then I turned to Discuss, only found that the top-voted one is actually using such a trick:
modify the code for pre-order traversal so that it becomes root->right->left, and then reverse the result to get left->right->root, so tricky!!!*/
public static List<Integer> postorderTraversal_iterative(TreeNode root) {
List<Integer> result = new ArrayList();
Stack<TreeNode> stack = new Stack();
if(root == null) return result;
stack.push(root);
while(!stack.isEmpty()){
root = stack.pop();
result.add(root.val);
if(root.left != null) stack.push(root.left);
if(root.right != null) stack.push(root.right);
}
Collections.reverse(result);
return result;
}
public static void main(String...strings){
// TreeNode root = new TreeNode(1);
// root.left = new TreeNode(2);
// root.right = new TreeNode(3);
// TreeNode root = new TreeNode(1);
// root.left = new TreeNode(2);
TreeNode root = new TreeNode(1);
root.right = new TreeNode(2);
List<Integer> result = postorderTraversal_iterative(root);
CommonUtils.printList(result);
}
public List<Integer> postorderTraversal_recursive(TreeNode root) {
List<Integer> result = new ArrayList();
return post(root, result);
}
List<Integer> post(TreeNode root, List<Integer> result){
if(root == null) return result;
post(root.left, result);
post(root.right, result);
result.add(root.val);
return result;
}
}