forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_107.java
39 lines (36 loc) · 1.22 KB
/
_107.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class _107 {
public static class Solution1 {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> result = new ArrayList();
if (root == null) {
return result;
}
Queue<TreeNode> q = new LinkedList();
q.offer(root);
while (!q.isEmpty()) {
List<Integer> thisLevel = new ArrayList<>();
int qSize = q.size();
for (int i = 0; i < qSize; i++) {
TreeNode curr = q.poll();
thisLevel.add(curr.val);
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
result.add(thisLevel);
}
Collections.reverse(result);
return result;
}
}
}