Serialize and Deserialize Binary Tree 07/25

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */


public class Solution {
    /**
     * This method will be invoked first, you should design your own algorithm 
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    public String serialize(TreeNode root) {
        // write your code here
        if(root == null)
        {           return "{}";        
            
        }                
        ArrayList<TreeNode> queue = new ArrayList<>();                
        queue.add(root);
        for(int i = 0; i < queue.size();++i){
            TreeNode node = queue.get(i);                        
            if(node == null){
                continue;            
                
            }                       
            queue.add(node.left);            
            queue.add(node.right);       
        }               
        while(queue.get(queue.size()-1) == null){
            queue.remove(queue.size()-1);
            }  
        StringBuilder s = new StringBuilder();
        s.append("{"); 
        s.append(queue.get(0).val);
        for(int i = 1; i < queue.size();++i){
            if(queue.get(i) == null){
                s.append(",#");            
                
            }else{
                s.append(",");
                s.append(queue.get(i).val);
                } 
            } 
            s.append("}");
            return s.toString();
       
    }

    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in 
     * "serialize" method.
     */
    public TreeNode deserialize(String data) {
        // write your code here
        
        if(data.equals("{}")){
            
            return null;
        }
        String[] val = data.substring(1,data.length()-1).split(",");
        List<TreeNode> list = new ArrayList<>(); 
    
        
        TreeNode root = new TreeNode(Integer.parseInt(val[0]));
        list.add(root);
        boolean isLeft = true;
        int index = 0; 
        for(int i = 1; i < val.length;i++){
            if(!val[i].equals("#")){
               
                TreeNode node = new TreeNode(Integer.parseInt(val[i]));                
                if(isLeft){
                    list.get(index).left = node;
                    }else{ 
                        list.get(index).right = node;
                    }
                    
                    list.add(node);
            }           
              if(!isLeft){
                 index++;
                } 
                isLeft = !isLeft;
        }    
                            return root;
       
    }
}

Last updated