-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconBinaryTree.java
More file actions
49 lines (44 loc) · 1.34 KB
/
conBinaryTree.java
File metadata and controls
49 lines (44 loc) · 1.34 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
package Interview;
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class conBinaryTree {
public static void main(String[] args){
TreeNode result = deserialize("[1,2,3]");
System.out.println();
}
public static TreeNode deserialize(String data) {
String[] nodes = data.substring(1, data.length()-2).split(",");
Queue<TreeNode> queue = new LinkedList<>();
TreeNode head = null;
try{
head = new TreeNode(Integer.parseInt(nodes[0]));
}catch(Exception e){
return null;
}
queue.offer(head);
for(int i=1; i<nodes.length; i++){
if(!nodes[i].equals("Null")){
try{
int val = Integer.parseInt(nodes[i]);
TreeNode tmpNode = new TreeNode(val);
queue.offer(tmpNode);
if(queue.peek().left==null){
queue.peek().left=tmpNode;
}else if(queue.peek().right==null){
queue.peek().right=tmpNode;
queue.poll();
}
}catch(Exception e){
continue;
}
}
}
return head;
}
}