-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM26.java
More file actions
77 lines (72 loc) · 2.23 KB
/
BM26.java
File metadata and controls
77 lines (72 loc) · 2.23 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package NiukeTOP101;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class BM26 {
/**
*
* @param root TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
public static ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
if(root == null) {
return ret;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
TreeNode index = root;
ArrayList<Integer> ans = new ArrayList<>();
while (!queue.isEmpty()){
TreeNode node = queue.pollFirst();
ans.add(node.val);
if(node == index){
ret.add(new ArrayList<>(ans));
ans.clear();
}
if(node.left != null){
queue.add(node.left);
}
if(node.right != null){
queue.add(node.right);
}
if(node == index && !queue.isEmpty()){
index = queue.getLast();
}
}
return ret;
}
/**
* 将一个arry数组构建成一个完全二叉树
* @param arr 需要构建的数组
* @return 二叉树的根节点
*/
public static TreeNode initBinTree(int[] arr) {
if(arr.length == 1) {
return new TreeNode(arr[0]);
}
List<TreeNode> nodeList = new ArrayList<>();
for(int i = 0; i < arr.length; i++) {
if(arr[i] == -1){
nodeList.add(null);
}else{
nodeList.add(new TreeNode(arr[i]));
}
}
int temp = 0;
while(temp <= (arr.length - 2) / 2) { //注意这里,数组的下标是从零开始的
if(2 * temp + 1 < arr.length) {
nodeList.get(temp).left = nodeList.get(2 * temp + 1);
}
if(2 * temp + 2 < arr.length) {
nodeList.get(temp).right = nodeList.get(2 * temp + 2);
}
temp++;
}
return nodeList.get(0);
}
public static void main(String[] args) {
TreeNode head = initBinTree(new int[]{3, 9, 20, -1, -1, 15, 7});
levelOrder(head);
}
}