forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoom2.java
More file actions
75 lines (58 loc) · 1.58 KB
/
Zoom2.java
File metadata and controls
75 lines (58 loc) · 1.58 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
package BiShi;
import sun.reflect.generics.tree.Tree;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @Author MaoTian
* @Classname Zoom2
* @Description TODO
* @Date 下午3:01 2019/8/17
* @Version 1.0
* @Created by mao<[email protected]>
*/
class TreeNode{
String val;
TreeNode left;
TreeNode right;
TreeNode(String val){
this.val=val;
}
}
public class Zoom2 {
TreeNode getTree(String s){
String[] arr=s.split(",");
ArrayList<TreeNode> tmp=new ArrayList<>();
TreeNode root=new TreeNode(arr[0]);
tmp.add(root);
int start=1;
while (!tmp.isEmpty()){
ArrayList<TreeNode> middle=new ArrayList<>();
for(TreeNode t:tmp){
if(start<arr.length){
TreeNode left=new TreeNode(arr[start++]);
middle.add(left);
t.left=left;
}else {
t.left=null;
}
if(start<arr.length){
TreeNode right=new TreeNode(arr[start++]);
middle.add(right);
t.right=right;
}else {
t.right=null;
}
}
tmp.clear();
tmp.addAll(middle);
}
return root;
}
public static void main(String[] args)throws Exception {
Scanner sc =new Scanner(new File("/home/mao/workspace/java/src/BiShi/test2"));
//Scanner sc =new Scanner(System.in);
while(sc.hasNext()){
}
}
}