-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_107.java
More file actions
35 lines (31 loc) · 1002 Bytes
/
Copy pathP_107.java
File metadata and controls
35 lines (31 loc) · 1002 Bytes
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
package leetcode.easy;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import utils.DataStructures.TreeNode;
public class P_107 {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
final Deque<TreeNode> queue = new LinkedList<>();
final List<List<Integer>> res = new ArrayList<>();
if (root != null) {
queue.offerLast(root);
}
while (!queue.isEmpty()) {
int levelSize = queue.size();
final List<Integer> level = new ArrayList<>();
while (levelSize-- > 0) {
final TreeNode curr = queue.removeFirst();
level.add(curr.val);
if (curr.left != null) {
queue.add(curr.left);
}
if (curr.right != null) {
queue.add(curr.right);
}
}
res.add(0, level);
}
return res;
}
}