-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path107-levelOrderBottom.java
More file actions
34 lines (32 loc) · 1021 Bytes
/
107-levelOrderBottom.java
File metadata and controls
34 lines (32 loc) · 1021 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
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
// 还是用队列来实现,结果插入的时候插的队头就可以了
class Solution_107 {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
LinkedList<List<Integer>> res = new LinkedList<List<Integer>>();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()) {
int count = queue.size();
LinkedList<Integer> one = new LinkedList<Integer>();
for(int i=0; i<count; i++){
TreeNode node = queue.poll();
one.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right != null){
queue.add(node.right);
}
}
res.addFirst(one);
}
return res;
}
}