Skip to content

Commit 6af276e

Browse files
authored
Update Serialize and Deserialize a Binary Tree.java
1 parent 814554b commit 6af276e

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

Hard/Serialize and Deserialize a Binary Tree.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,50 @@
99
*/
1010
public class Codec {
1111

12-
// Encodes a tree to a single string.
13-
public String serialize(TreeNode root) {
14-
StringBuilder sb = new StringBuilder();
15-
serializeHelper(root, sb);
16-
return sb.toString().substring(0, sb.length() - 1);
12+
private static final String NULL_VALUE = "-";
13+
private static final String DELIMETER = " ";
14+
15+
// Encodes a tree to a single string.
16+
public String serialize(TreeNode root) {
17+
if (root == null) {
18+
return NULL_VALUE;
1719
}
18-
19-
private void serializeHelper(TreeNode root, StringBuilder sb) {
20-
if (root == null) {
21-
sb.append("#").append(",");
22-
return;
23-
}
24-
25-
sb.append(root.val).append(",");
26-
serializeHelper(root.left, sb);
27-
serializeHelper(root.right, sb);
20+
StringBuilder sb = new StringBuilder();
21+
Stack<TreeNode> stack = new Stack<>();
22+
stack.add(root);
23+
while (!stack.isEmpty()) {
24+
TreeNode removed = stack.pop();
25+
if (removed == null) {
26+
sb.append(NULL_VALUE).append(DELIMETER);
27+
continue;
28+
}
29+
sb.append(removed.val).append(DELIMETER);
30+
stack.push(removed.right);
31+
stack.push(removed.left);
2832
}
33+
return sb.toString().trim();
34+
}
2935

30-
// Decodes your encoded data to tree.
31-
public TreeNode deserialize(String data) {
32-
Queue<String> values = new LinkedList<>(Arrays.asList(data.split(",")));
33-
return deserializeHelper(values);
34-
}
35-
36-
private TreeNode deserializeHelper(Queue<String> values) {
37-
if (values.isEmpty()) {
38-
return null;
39-
}
40-
41-
String val = values.remove();
42-
if (val.equals("#")) {
43-
return null;
44-
}
45-
46-
TreeNode root = new TreeNode(Integer.parseInt(val));
47-
root.left = deserializeHelper(values);
48-
root.right = deserializeHelper(values);
49-
50-
return root;
36+
// Decodes your encoded data to tree.
37+
public TreeNode deserialize(String data) {
38+
Queue<String> queue = new LinkedList<>(Arrays.asList(data.split("\\s+")));
39+
return helper(queue);
40+
}
41+
42+
private TreeNode helper(Queue<String> queue) {
43+
if (queue.peek().equals(NULL_VALUE)) {
44+
queue.remove();
45+
return null;
5146
}
47+
TreeNode root = new TreeNode(Integer.parseInt(queue.peek()));
48+
queue.remove();
49+
root.left = helper(queue);
50+
root.right = helper(queue);
51+
return root;
52+
}
5253
}
5354

5455
// Your Codec object will be instantiated and called as such:
55-
// Codec codec = new Codec();
56-
// codec.deserialize(codec.serialize(root));
56+
// Codec ser = new Codec();
57+
// Codec deser = new Codec();
58+
// TreeNode ans = deser.deserialize(ser.serialize(root));

0 commit comments

Comments
 (0)