-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Level Order Traversal II.java
36 lines (30 loc) · 1.26 KB
/
Binary Tree Level Order Traversal II.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
//http://blog.csdn.net/likecool21/article/details/24289447
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<TreeNode>> result = new ArrayList<List<TreeNode>>();
List<List<Integer>> trueResult = new ArrayList<List<Integer>>();
if(root == null)
return trueResult;
ArrayList<TreeNode> current = new ArrayList<TreeNode>();
current.add(root);
while(current.size() > 0){
//add the previous level into the final result
result.add(current);
ArrayList<TreeNode> parent = current;
current = new ArrayList<TreeNode>();
for(TreeNode node: parent){
if(node.left != null)
current.add(node.left);
if(node.right != null)
current.add(node.right);
}
}
int len = result.size();
for(int i = len-1;i>=0;i--){
ArrayList<Integer> intArr = new ArrayList<Integer>();
for(TreeNode node:result.get(i)){
intArr.add(node.val);
}
trueResult.add(intArr);
}
return trueResult;
}