-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0297-serialize-and-deserialize-binary-tree.java
More file actions
65 lines (62 loc) · 2.12 KB
/
0297-serialize-and-deserialize-binary-tree.java
File metadata and controls
65 lines (62 loc) · 2.12 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
/**
* 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 res = "";
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
for (int i=0; i<size; i++) {
TreeNode curr = q.poll();
if (curr == null) {
res += "n,";
continue;
}
else {
res += String.valueOf(curr.val);
res += ",";
q.offer(curr.left);
q.offer(curr.right);
}
}
}
return res;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length() == 0) return null;
Queue<TreeNode> q = new LinkedList<>();
String[] values = data.split(",");
TreeNode root = new TreeNode(Integer.parseInt(values[0]));
q.offer(root);
for (int i=1; i<values.length; i++) {
TreeNode curr = q.poll();
if (!values[i].equals("n")) {
TreeNode leftChild = new TreeNode(Integer.parseInt(values[i]));
curr.left = leftChild;
q.offer(leftChild);
}
i++;
if (!values[i].equals("n")) {
TreeNode rightChild = new TreeNode(Integer.parseInt(values[i]));
curr.right = rightChild;
q.offer(rightChild);
}
}
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));