-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_145.java
More file actions
71 lines (62 loc) · 1.97 KB
/
Solution_145.java
File metadata and controls
71 lines (62 loc) · 1.97 KB
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
/*
145. 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
*/
import java.util.*;
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
TreeNode[] nodes = new TreeNode[]{
new TreeNode(1),new TreeNode(2),new TreeNode(3),new TreeNode(4)
};
nodes[0].right = nodes[1];
nodes[1].left = nodes[2];
nodes[2].left = nodes[3];
System.out.println(solution.postorderTraversal(nodes[0]));
}
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null)
return res;
if ( root.left == null && root.right == null ) {
res.add(root.val);
return res;
}
Map<TreeNode, Integer> map = new HashMap<>();
LinkedList<TreeNode> stack = new LinkedList<>();
stack.add(root);
while ( !stack.isEmpty() ) {
TreeNode cur = stack.getLast();
System.out.println(cur.val+" "+map.getOrDefault(cur, -1));
for ( int i = 0 ; i < stack.size() ; i++ )
System.out.print(stack.get(i).val+" ");
System.out.println();
if( !map.containsKey(cur) )
map.put(cur, 1);
else
map.replace(cur, map.get(cur)+1);
if ( map.get(cur) == 3 ) {
res.add(cur.val);
stack.pollLast();
} else if ( cur.left != null && map.get(cur) == 1 ) {
stack.add(cur.left);
} else if ( cur.right != null ) {
stack.add(cur.right);
map.replace(cur, 2);
} else {
res.add(cur.val);
stack.pollLast();
}
}
return res;
}
}