
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.
So the first question is: what is the difference between this and #297?
This here is BST, however, in #297, it's BT. "The encoded string should be as compact as possible" here. The special property of binary search trees compared to general binary trees allows a more compact encoding. So while the solutions to problem #297 still work here as well, they're not as good as they should be.
For general BT, to reconstruct the tree, we need the information of both in-order and pre-order, or in-order and post-order. We've practised that already.
However, as a BST, just the information of pre-order or post-order is sufficient to rebuild the tree.
The encoded string is also most compact, since we do not need to keep tract of information of 'Null nodes'.
Example: 4
2 6
1 3 5 7
The pre-order encoding is: "4213657". It is easy to tell "4" is root, "213" is left tree, "657" is right tree. We can use a Queue to implement this, very convenient.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec { // Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) return "";
StringBuilder res = new StringBuilder();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node = root;
while (!stack.isEmpty() || node!=null) {
if (node != null) {
res.append(node.val).append(" ");
stack.push(node);
node = node.left;
}
else {
node = stack.pop().right;
}
}
return res.toString().trim();
} // Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data==null || data.length()==0) return null;
String[] nodes = data.split(" ");
Queue<Integer> queue = new LinkedList<>();
for (String node : nodes) {
queue.offer(Integer.valueOf(node));
}
return buildTree(queue);
} public TreeNode buildTree(Queue<Integer> queue) {
if (queue.isEmpty()) return null;
TreeNode root = new TreeNode(queue.poll());
Queue<Integer> leftNodeVals = new LinkedList<>();
while (!queue.isEmpty() && queue.peek()<=root.val) {
leftNodeVals.offer(queue.poll());
}
root.left = buildTree(leftNodeVals);
root.right = buildTree(queue);
return root;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));