-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
70 lines (54 loc) · 1.6 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
public String serialize(TreeNode root) {
if (root == null) {
return "";
}
StringBuilder sb = new StringBuilder();
Deque<TreeNode> deque = new LinkedList<>();
deque.add(root);
while (!deque.isEmpty()) {
TreeNode p = deque.pop();
if (p == null) {
sb.append(",#");
} else {
sb.append(",").append(p.val);
deque.add(p.left);
deque.add(p.right);
}
}
return sb.toString().substring(1);
}
public TreeNode deserialize(String data) {
if (data == null || Objects.equals(data, "")) {
return null;
}
String[] s = data.split(",");
TreeNode[] root = new TreeNode[s.length];
for (int i = 0; i < root.length; i++) {
if (!Objects.equals(s[i], "#")) {
root[i] = new TreeNode(Integer.valueOf(s[i]));
}
}
int parent = 0;
for (int i = 0; parent * 2 + 2 < s.length; i++) {
if (root[i] != null) {
root[i].left = root[parent * 2 + 1];
root[i].right = root[parent * 2 + 2];
parent++;
}
}
return root[0];
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));