Skip to content

Commit e8aa56e

Browse files
authored
Update SerializeDeserializeBST.java
1 parent 488fdee commit e8aa56e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

SerializeDeserializeBST.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import java.util.*;
22

3+
/**
4+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored
5+
in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or
6+
another computer environment.
7+
8+
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your
9+
serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized
10+
to a string and this string can be deserialized to the original tree structure.
11+
12+
The encoded string should be as compact as possible.
13+
14+
Note: Do not use class member/global/static variables to store states.
15+
Your serialize and deserialize algorithms should be stateless.
16+
*/
317
public class SerializeDeserializeBST {
418
public static class TreeNode {
519
int val;
@@ -28,6 +42,7 @@ public static void main(String args[]){
2842
}
2943

3044
private static String serialize(TreeNode root) {
45+
if(root == null) return null;
3146
Queue<Integer> q = new LinkedList<>();
3247
serialize(root, q);
3348
String result = "";
@@ -45,6 +60,7 @@ private static void serialize(TreeNode root, Queue<Integer> q) {
4560
}
4661

4762
private static TreeNode deserialize(String serialized) {
63+
if(serialized == null) return null;
4864
String[] array = serialized.split("#");
4965
Queue<Integer> q = new LinkedList<>();
5066

0 commit comments

Comments
 (0)