-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
81 lines (69 loc) · 2.26 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
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
// https://leetcode.com/problems/serialize-and-deserialize-binary-tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null){
return "";
}
Queue<TreeNode> q = new LinkedList<>();
String ans = root.val + " ";
q.add(root);
while(q.size() > 0){
TreeNode node = q.remove();
if(node.left != null){
q.add(node.left);
ans += node.left.val + " ";
}else{
ans += "null ";
}
if(node.right != null){
q.add(node.right);
ans += node.right.val + " ";
}else{
ans += "null ";
}
}
// System.out.println(ans);
return ans.substring(0, ans.length()-1);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data.equals("")){
return null;
}
String[] nodes = data.split(" ");
TreeNode root = new TreeNode(Integer.parseInt(nodes[0]));
LinkedList<TreeNode> list = new LinkedList<>();
list.add(root);
int i=1;
while(i<nodes.length-1 && list.size() > 0){
TreeNode temp = list.remove();
// System.out.println(temp.val + " " + nodes[i] + " " + nodes[i+1]);
if(!nodes[i].equals("null")){
TreeNode left = new TreeNode(Integer.parseInt(nodes[i]));
temp.left = left;
list.add(left);
}
if(!nodes[i+1].equals("null")){
TreeNode right = new TreeNode(Integer.parseInt(nodes[i+1]));
temp.right = right;
list.add(right);
}
i += 2;
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));