-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain107.java
More file actions
33 lines (29 loc) · 919 Bytes
/
Main107.java
File metadata and controls
33 lines (29 loc) · 919 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
package HOT100;
import java.util.*;
public class Main107 {
private List<List<Integer>> lists = new ArrayList<>();
private Queue<TreeNode> Queue = new LinkedList<>();
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if(root==null){
return lists;
}
Queue.add(root);
while (!Queue.isEmpty()){
List<Integer> level = new ArrayList<>();
int currentLevelSize = Queue.size();
for(int i=1; i<=currentLevelSize; ++i){
TreeNode node = Queue.poll();
level.add(node.val);
if(node.left!=null){
Queue.offer(node.left);
}
if(node.right!=null){
Queue.offer(node.right);
}
}
lists.add(level);
}
Collections.reverse(lists);
return lists;
}
}