-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution145.java
59 lines (52 loc) · 1.5 KB
/
Solution145.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
package algorithm.leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* @author: mayuan
* @desc: 二叉树的后序遍历
* @date: 2019/03/09
*/
public class Solution145 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new LinkedList<>();
if (null == root) {
return ans;
}
LinkedList<TreeNode> stack = new LinkedList<>();
// cur:当前访问节点
TreeNode cur = root;
// pLastVisit:上次访问节点
TreeNode pLastVisit = null;
// 先移动到最左子树下,过程中压入栈
while (null != cur) {
stack.push(cur);
cur = cur.left;
}
while (!stack.isEmpty()) {
cur = stack.pop();
//访问根节点的前提是:无右子树或右子树已被访问过
if (null == cur.right || pLastVisit == cur.right) {
ans.add(cur.val);
pLastVisit = cur;
} else {
// 根节点再次入栈
stack.push(cur);
// 进入右子树,且可肯定右子树一定不为空
cur = cur.right;
while (null != cur) {
stack.push(cur);
cur = cur.left;
}
}
}
return ans;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}