-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializeAndDeserialize.java
More file actions
134 lines (110 loc) · 3.65 KB
/
SerializeAndDeserialize.java
File metadata and controls
134 lines (110 loc) · 3.65 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.LinkedList;
import java.util.Queue;
public class SerializeAndDeserialize {
/**
* This method will be invoked first, you should design your own algorithm to
* serialize a binary tree which denote by a root node to a string which can be
* easily deserialized by your own "deserialize" method later.
*/
public String serialize(TreeNode root) {
// write your code here
if (root == null) {
return "{}";
}
StringBuilder sb = new StringBuilder();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
sb.append("{");
sb.append(Integer.toString(root.val));
while (!q.isEmpty()) {
TreeNode node = q.poll();
if (node.left != null) {
sb.append(",");
sb.append(Integer.toString(node.left.val));
q.offer(node.left);
} else if (node.left == null) {
sb.append(",#");
}
if (node.right != null) {
sb.append(",");
sb.append(Integer.toString(node.right.val));
q.offer(node.right);
} else if (node.right == null) {
sb.append(",#");
}
if (q.isEmpty()) {
sb.append("}");
}
}
// for (int i = sb.length() - 2; i >= 0; i--) {
// if (sb.charAt(i) == '#') {
// sb.delete(i - 1, i + 1);
// }
// else {
// break;
// }
// }
int flag = sb.length() - 2;
while(flag >= 0) {
if (sb.charAt(flag) == '#') {
flag -= 2;
}
else {
break;
}
}
sb.delete(flag + 1, sb.length() - 1);
return sb.toString();
}
/**
* This method will be invoked second, the argument data is what exactly you
* serialized at method "serialize", that means the data is not given by system,
* it's given by your own serialize method. So the format of data is designed by
* yourself, and deserialize it here as you serialize it in "serialize" method.
*/
public TreeNode deserialize(String data) {
// write your code here
if (data.equals("{}")) {
return null;
}
String[] vals = data.substring(1, data.length() - 1).split(",");
Queue<TreeNode> q = new LinkedList<>();
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
q.offer(root);
boolean isLeftChild = true;
for (int i = 1; i < vals.length; i++) {
if (!vals[i].equals("#")) {
TreeNode newNode = new TreeNode(Integer.parseInt(vals[i]));
q.offer(newNode);
if (isLeftChild) {
q.peek().left = newNode;
} else {
q.peek().right = newNode;
q.poll();
}
}
else {
if (!isLeftChild) {
q.poll();
}
}
isLeftChild = !isLeftChild;
}
return root;
}
public static void main(String[] args) {
SerializeAndDeserialize s = new SerializeAndDeserialize();
String input = "{3,9,20,#,#,15,7}";
TreeNode t = s.deserialize(input);
String output = s.serialize(t);
System.out.println(output);
}
}
class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}