注:题目及解法均来自于LeetCode
题目描述
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.
AC代码1
常规解法,serialize的时候保存的是BST的先序遍历,节点与节点之间用”,”隔开。
deserialize的时候根据先序遍历的性质,serialize后的结果的第一位即是根节点,比根节点小的序列是根节点的左子树,比根节点大的是根节点的右子树,递归地找到左子树和右子树的根节点即可构造整棵树。
public class _449_Se_Derialize {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
static final String SEP = ",";
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder stringBuilder = new StringBuilder();
if(root == null) return null;
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
while(!stack.isEmpty()){
root = stack.pop();
stringBuilder.append(root.val).append(SEP);
if(root.right != null) stack.push(root.right);//因为stack先进后出的性质,要先存储右节点,在存储左节点,这样先输出的是左节点。
if(root.left != null) stack.push(root.left);
}
return stringBuilder.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == null) return null;
String[] strs = data.split(SEP);
Queue<Integer> queue = new LinkedList<>();
for(String s : strs) queue.offer(Integer.parseInt(s));
return getNode(queue);
}
private TreeNode getNode(Queue<Integer> queue){
if(queue.isEmpty()) return null;
TreeNode root = new TreeNode(queue.poll());
Queue<Integer> smallQueue = new LinkedList<>();
while(!queue.isEmpty() && queue.peek() < root.val)
smallQueue.offer(queue.poll());
root.left = getNode(smallQueue);
root.right = getNode(queue);
return root;
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
}
AC代码2
思路与解法1相同,只不过更为精简,没有用到 stack 和 queue 的数据结构。
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root==null) return "";
StringBuilder sb = new StringBuilder();
seriHelper(root, sb);
return sb.toString();
}
private void seriHelper(TreeNode root, StringBuilder sb){
if (root==null) return;
sb.append((char)root.val);
seriHelper(root.left, sb);
seriHelper(root.right, sb);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
return deseHelper(data.toCharArray(), Integer.MIN_VALUE, Integer.MAX_VALUE, new int[1]);
}
private TreeNode deseHelper(char[] data, int min, int max, int[] idx){
if (idx[0]>=data.length || data[idx[0]]>max || data[idx[0]]<min) return null;
TreeNode root = new TreeNode(data[idx[0]]);
idx[0]++;
root.left = deseHelper(data, min, root.val, idx);
root.right = deseHelper(data, root.val, max, idx);
return root;
}
}
AC代码3
特别聪明而且让人哭笑不得的代码:
public class Codec {
TreeNode node;
public String serialize(TreeNode root) {
node=root;
return "";
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
return node;
}
}