-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Serialization.java
188 lines (168 loc) Β· 5.19 KB
/
Binary Tree Serialization.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is
called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.
Example
An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:
3
/ \
9 20
/ \
15 7
Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.
You can use other method to do serializaiton and deserialization.
time = O(n)
space = O(n) for the list needed, and stringbuilder
*/
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
public String serialize(TreeNode root) {
if (root == null) {
return "{}";
}
// don't use queue here, for adding null in the list
List<TreeNode> list = new ArrayList<>();
list.add(root);
for (int i = 0; i < list.size(); i++) {
TreeNode node = list.get(i);
if (node == null) {
continue;
}
list.add(node.left);
list.add(node.right);
}
// remove # at the end of list
while (list.get(list.size() - 1) == null) {
list.remove(list.size() - 1);
}
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(list.get(0).val);
for (int i = 1; i < list.size(); i++) {
if (list.get(i) == null) {
sb.append(",#");
} else {
sb.append(",");
sb.append(list.get(i).val);
}
}
sb.append("}");
return sb.toString();
}
public TreeNode deserialize(String data) {
// write your code here
if (data.equals("{}")) {
return null;
}
// substring to avoid "{","}"
String[] vals = data.substring(1, data.length() - 1).split(",");
List<TreeNode> list = new ArrayList<>();
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
list.add(root);
int cur = 0;
boolean isLeft = true;
for (int i = 1; i < vals.length; i++) {
// the left is not null for the first round
if (!vals[i].equals("#")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (isLeft) {
list.get(cur).left = node;
} else {
list.get(cur).right = node;
}
list.add(node);
}
if (!isLeft) {
cur++;
}
isLeft = !isLeft;
}
return root;
}
}
// leetcode version
/**
* 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 "{}";
}
// queue cannot be null
List<TreeNode> list = new ArrayList<>();
list.add(root);
for (int i = 0; i < list.size(); i++) {
TreeNode cur = list.get(i);
if (cur != null) {
list.add(cur.left);
list.add(cur.right);
}
}
// remove null at the tail of list
while (list.get(list.size() - 1) == null) {
list.remove(list.size() - 1);
}
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(list.get(0).val);
// for the ',' sign
for (int i = 1; i < list.size(); i++) {
if (list.get(i) == null) {
sb.append(",null");
} else {
sb.append(",");
sb.append(list.get(i).val);
}
}
sb.append("}");
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.equals("{}")) {
return null;
}
String[] vals = data.substring(1, data.length() - 1).split(",");
List<TreeNode> list = new ArrayList<>();
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
list.add(root);
int cur = 0;
boolean left = true;
for (int i = 1; i < vals.length; i++) {
if (!vals[i].equals("null")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (left) {
list.get(cur).left = node;
} else {
list.get(cur).right = node;
}
list.add(node);
}
// current is right, so forward to next layer
if (!left) {
cur++;
}
left = !left;
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));