-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path145.binary-tree-postorder-traversal.java
127 lines (110 loc) · 3.42 KB
/
145.binary-tree-postorder-traversal.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import javax.swing.tree.TreeNode;
import jdk.nashorn.internal.ir.TryNode;
/*
* @lc app=leetcode id=145 lang=java
*
* [145] Binary Tree Postorder Traversal
*/
// @lc code=start
/**
* Definition for a binary tree node. 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; } }
*/
// class Solution {
// public List<Integer> postorderTraversal(TreeNode root) {
// List<Integer> res = new ArrayList<>();
// postorder(root, res);
// return res;
// }
// public void postorder(TreeNode root, List<Integer> res) {
// if (root == null) {
// return;
// }
// postorder(root.left, res);
// postorder(root.right, res);
// res.add(root.val);
// }
// }
// class Solution {
// public List<Integer> postorderTraversal(TreeNode root) {
// List<Integer> res = new ArrayList<>();
// Deque<TreeNode> stack = new LinkedList<>();
// TreeNode pre = null;
// while (root != null || !stack.isEmpty()) {
// while (root != null) {
// stack.push(root);
// root = root.left;
// }
// root = stack.poll();
// if (root.right == null || root.right == pre) {
// res.add(root.val);
// pre = root;
// root = null;
// } else {
// stack.push(root);
// root = root.right;
// }
// }
// return res;
// }
// }
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
TreeNode node = root, pre = null;
while (node != null) {
if (node.left != null) {
// find the rightmost in node's left subtree
pre = node.left;
while (pre.right != null && pre.right != node) {
pre = pre.right;
}
// if this rightmost's right point to null
// then let this rightmost's right point to node
// aka let this rightmost become node's prenode
if (pre.right == null) {
pre.right = node;
// go left, move to next node
node = node.left;
// skip below "node = node.right;"
continue;
} else {
// if his rightmost's right point to node,
// let it point to null
pre.right = null;
// add path this left subtree
addPath(res, node.left);
}
}
// cannot use else!!! because the "continue;" above!!!
node = node.right;
}
addPath(res, root);
return res;
}
public void addPath(List<Integer> res, TreeNode node) {
// get the size of both left and right subtree
int count = 0;
while (node != null) {
count++;
res.add(node.val);
node = node.right;
}
int leftSubtreeSize = res.size() - count, rightSubtreeSize = res.size() - 1;
while (leftSubtreeSize < rightSubtreeSize) {
// swap
int temp = res.get(leftSubtreeSize);
res.set(leftSubtreeSize, res.get(rightSubtreeSize));
res.set(rightSubtreeSize, temp);
leftSubtreeSize++;
rightSubtreeSize--;
}
}
}
// @lc code=end