-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbinaryTreeLevelOrderTraversalII.java
More file actions
38 lines (36 loc) · 1.07 KB
/
binaryTreeLevelOrderTraversalII.java
File metadata and controls
38 lines (36 loc) · 1.07 KB
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
/*
Given a binary tree, return the bottom-up level order traversal of its nodes'
values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
*/
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(root == null) return result;
queue.offer(root);
while(!queue.isEmpty()){
List<Integer> list = new ArrayList<>();
int treeLevel = queue.size(); //element in each level
for(int i = 0; i < treeLevel; i++){
if(queue.peek().left != null) queue.offer(queue.peek().left);
if(queue.peek().right != null) queue.offer(queue.peek().right);
list.add(queue.poll().val);
}
result.add(0, list);
}
return result;
}
}