-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain102.java
More file actions
96 lines (85 loc) · 2.59 KB
/
Main102.java
File metadata and controls
96 lines (85 loc) · 2.59 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package HOT100;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Main102 {
private List<List<Integer>> lists = new ArrayList<>();
private Queue<TreeNode> Queue = new LinkedList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
if(root==null){
return lists;
}
Queue.add(root);
while (!Queue.isEmpty()){
List<Integer> level = new ArrayList<>();
int currentLevelSize = Queue.size();
for(int i=1; i<=currentLevelSize; ++i){
TreeNode node = Queue.poll();
level.add(node.val);
if(node.left!=null){
Queue.offer(node.left);
}
if(node.right!=null){
Queue.offer(node.right);
}
}
lists.add(level);
}
return lists;
}
public static void main(String[] args) {
String str = "[3,9,20,null,null,15,7]";
}
private static int[] StrToIntArray(String str){
str = str.substring(1, str.length()-1);
String[] strs = str.split(",");
int[] arr = new int[strs.length];
for (int i=0; i<arr.length; i++){
if(strs[i].equals("null")){
arr[i] = Integer.MAX_VALUE;
}else {
arr[i] = Integer.parseInt(strs[i]);
}
}
return arr;
}
private static TreeNode mkTree(String str){
int[] arr = StrToIntArray(str);
TreeNode[] nodes = new TreeNode[arr.length+1];
for(int i=1; i< nodes.length; i++){
if(arr[i-1] != Integer.MAX_VALUE){
nodes[i] = new TreeNode(arr[i-1]);
}else{
nodes[i] = null;
}
}
TreeNode node = null;
for(int i=1; i< nodes.length/2; i++){
node = nodes[i];
if(node == null) continue;
node.left=nodes[2*i];
node.right=nodes[2*i+1];
}
return nodes[1];
}
}
class Main102_1{
List<List<Integer>> list = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root){
if(root==null) return list;
dfs(root, 0);
return list;
}
public void dfs(TreeNode root, int level){
if(list.size()==level) list.add(new ArrayList<>());
list.get(level).add(root.val);
if(root.left!=null){
dfs(root.left, level+1);
}
if(root.right!=null){
dfs(root.right, level+1);
}
return;
}
}