-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.java
51 lines (47 loc) · 1.36 KB
/
Solution.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package $107;
import datastruc.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* @author Junlan Shuai[[email protected]].
* @date Created on 2:55 PM 2018/08/16.
*/
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root){
LinkedList<List<Integer>> lists = new LinkedList<>();
if (root == null){
return lists;
}
List list = new ArrayList();
list.add(root);
bfs(lists, list);
return lists;
}
private void bfs(LinkedList<List<Integer>> lists, List<TreeNode> list){
if (list.size() == 0){
return;
}
List<Integer> store = new ArrayList<>();
int size = list.size();
for (int i = 0; i < size; i++){
TreeNode temp = list.remove(0);
store.add(temp.val);
if (null != temp.left){
list.add(temp.left);
}
if (null != temp.right){
list.add(temp.right);
}
}
lists.addFirst(store);
bfs(lists, list);
}
public static void main(String[] args) {
Solution solution = new Solution();
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
solution.levelOrderBottom(root);
}
}