-
Notifications
You must be signed in to change notification settings - Fork 43
/
BinaryTreeLevelOrderTraversal_ii.java
117 lines (88 loc) · 3.26 KB
/
BinaryTreeLevelOrderTraversal_ii.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
package LeetCodeJava.BFS;
// https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
import LeetCodeJava.DataStructure.TreeNode;
import java.util.*;
public class BinaryTreeLevelOrderTraversal_ii {
// V0
// IDEA : BFS
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
List<List<Integer>> resFinal = new ArrayList<>();
if (root == null){
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
int layer = 0;
queue.add(root);
while (!queue.isEmpty()){
/** NOTE !!! we add new null array before for loop */
res.add(new ArrayList<>());
/** NOTE !!! we get queue size before for loop */
int q_size = queue.size();
for (int i = 0; i < q_size; i++){
TreeNode cur = queue.remove();
res.get(layer).add(cur.val);
if (cur.left != null){
queue.add(cur.left);
}
if (cur.right != null){
queue.add(cur.right);
}
}
layer += 1;
}
for (int j = res.size()-1; j >= 0; j--){
List<Integer> cur = res.get(j);
//resFinal.add(new ArrayList<>());
resFinal.add(cur);
}
return resFinal;
}
// V1
// IDEA : BFS
public List<List<Integer>> levelOrderBottom_2(TreeNode root) {
List<List<Integer>> levels = new ArrayList<List<Integer>>();
if (root == null) return levels;
ArrayDeque<TreeNode> nextLevel = new ArrayDeque() {{ offer(root); }};
ArrayDeque<TreeNode> currLevel = new ArrayDeque();
while (!nextLevel.isEmpty()) {
currLevel = nextLevel.clone();
nextLevel.clear();
levels.add(new ArrayList<Integer>());
for (TreeNode node : currLevel) {
// append the current node value
levels.get(levels.size() - 1).add(node.val);
// process child nodes for the next level
if (node.left != null)
nextLevel.offer(node.left);
if (node.right != null)
nextLevel.offer(node.right);
}
}
/** NOTE !!! we can reverse list via Collections.reverse */
Collections.reverse(levels);
return levels;
}
// V2
// IDEA : RECURSION
// https://leetcode.com/problems/binary-tree-level-order-traversal-ii/editorial/
List<List<Integer>> levels = new ArrayList<List<Integer>>();
public void helper(TreeNode node, int level) {
// start the current level
if (levels.size() == level)
levels.add(new ArrayList<Integer>());
// append the current node value
levels.get(level).add(node.val);
// process child nodes for the next level
if (node.left != null)
helper(node.left, level + 1);
if (node.right != null)
helper(node.right, level + 1);
}
public List<List<Integer>> levelOrderBottom_3(TreeNode root) {
if (root == null) return levels;
helper(root, 0);
Collections.reverse(levels);
return levels;
}
}