-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializeDeserializeBST.java
84 lines (72 loc) · 2.89 KB
/
SerializeDeserializeBST.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
import java.util.*;
/**
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored
in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or
another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your
serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized
to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states.
Your serialize and deserialize algorithms should be stateless.
*/
public class SerializeDeserializeBST {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public static TreeNode getValidTree(){
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.left.left = new TreeNode(3);
root.left.left.left = new TreeNode(1);
root.left.left.right = new TreeNode(4);
root.left.right = new TreeNode(6);
root.right = new TreeNode(15);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(20);
return root;
}
public static void main(String args[]){
System.out.println("Serialize : " + serialize(getValidTree()));
TreeNode node = deserialize(serialize(getValidTree()));
System.out.println("deserialize : " + node.val);
}
private static String serialize(TreeNode root) {
if(root == null) return null;
Queue<Integer> q = new LinkedList<>();
serialize(root, q);
String result = "";
while (!q.isEmpty()){
result += (result.isEmpty()) ? q.poll() : "#" + q.poll();
}
return result;
}
private static void serialize(TreeNode root, Queue<Integer> q) {
if(root == null) return;
q.add(root.val);
serialize(root.left, q);
serialize(root.right, q);
}
private static TreeNode deserialize(String serialized) {
if(serialized == null) return null;
String[] array = serialized.split("#");
Queue<Integer> q = new LinkedList<>();
for(String str : array){
q.add(Integer.valueOf(str));
}
return deserialize(q, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private static TreeNode deserialize(Queue<Integer> q, int min, int max) {
if(q.isEmpty()) return null;
int next = q.peek();
if(next<min || next>max) return null;
q.poll();
TreeNode root = new TreeNode(next);
root.left = deserialize(q, min, next);
root.right = deserialize(q, next, max);
return root;
}
}