-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path107.java
29 lines (29 loc) · 896 Bytes
/
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> result = new LinkedList<List<Integer>>();
if(root==null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(queue.size()>0){
List<Integer> temp = new ArrayList<Integer>();
int len = queue.size();
for(int i=0;i<len;i++){
TreeNode node = queue.poll();
temp.add(node.val);
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
}
result.add(0,temp);
}
return result;
}
}