Serialize and Deserialize BST

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.

和一般的二叉树加密没区别, 时间复杂度 o(n),

/**
 * 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 builder = new StringBuilder();
        
        List<TreeNode> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        
        queue.offer(root);
        
        while(!queue.isEmpty()){
            int size = queue.size();
            
            for(int i = 0; i < size;i++){
                TreeNode node = queue.poll();
                
                list.add(node);
                
                if(node == null){
                    continue;
                }
                
                queue.offer(node.left);
                queue.offer(node.right);
                
            }
        }
        
        
        while(list.get(list.size()-1) ==null){
            list.remove(list.size()-1);
        }
        
        builder.append(list.get(0).val);
        
        for(int i = 1; i < list.size() ;i++){
            builder.append(",");
            if(list.get(i) == null){
                builder.append("#");
            }else{
                builder.append(list.get(i).val);
            }
        }
        
        return builder.toString();
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data == null || data.length() == 0){
            return null;
        }
        
        String[] strArr = data.split(",");
        
        TreeNode root = new TreeNode(Integer.parseInt(strArr[0]));
        
        Queue<TreeNode> queue = new LinkedList<>();
        
        
        queue.offer(root);
        int index = 1;
        while(!queue.isEmpty() && index < strArr.length){
          
           TreeNode node = queue.poll();
            
            TreeNode left = null;
            TreeNode right = null;
            
            if(index < strArr.length && !strArr[index].equals("#")){
                left = new TreeNode(Integer.parseInt(strArr[index]));
                queue.offer(left);
            }
            
            index++;
            
            if(index<strArr.length && !strArr[index].equals("#")){
                right = new TreeNode(Integer.parseInt(strArr[index]));
                 queue.offer(right);
            }
            
            index++;
            
            node.left = left;
            node.right = right;
            
//             if(node.left != null){
//                 queue.offer(node.left);
//             }
            
//             if(node.right != null){
//                 queue.offer(node.right);
//             }
        }
        
        return root;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

Last updated