449. Serialize and Deserialize BST
Serialization is 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 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.
Example 1:
Input: root = [2,1,3]
Output: [2,1,3]
Example 2:
Input: root = []
Output: []
Constraints:
- The number of nodes in the tree is in the range [0, 104].
- 0 <= Node.val <= 104
- The input tree is guaranteed to be a binary search tree.
BFS
/**
* 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 sb = new StringBuilder("");
Queue<TreeNode> q = new LinkedList();
q.add(root);
while(q.size() > 0) {
TreeNode node = q.poll();
if(node == null) {
sb.append("n ");
continue;
}
sb.append(node.val + " ");
q.add(node.left);
q.add(node.right);
}
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == "") return null;
String[] s = data.split(" ");
TreeNode root = new TreeNode(Integer.parseInt(s[0]));
Queue<TreeNode> q = new LinkedList();
q.add(root);
for(int i = 1; i < s.length; i++) {
TreeNode parent = q.poll();
if(!s[i].equals("n")) {
parent.left = new TreeNode(Integer.parseInt(s[i]));
q.add(parent.left);
}
i++;
if(i < s.length && !s[i].equals("n")) {
parent.right = new TreeNode(Integer.parseInt(s[i]));
q.add(parent.right);
}
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// String tree = ser.serialize(root);
// TreeNode ans = deser.deserialize(tree);
// return ans;
/**
* 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 sb = new StringBuilder();
preorder(root, sb);
}
private void preorder(TreeNode root, StringBuilder sb) {
if(root == null){
return;
}
sb.append(root.val + " ");
preorder(root.left, sb);
preorder(root.right, sb);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data.isEmpty()){
return null;
}
String[] s = data.split(" ");
return constructBST(s, Integer.MAX_VALUE);
}
int i = 0;
private TreeNode constructBST(String[] s, int upper) {
if(i==s.length || Integer.parseInt(s[i]) > upper){
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(s[i++]));
root.left = constructBST(s, root.val);
root.right = constructBST(s, upper);
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// String tree = ser.serialize(root);
// TreeNode ans = deser.deserialize(tree);
// return ans;