queue + levelorder to serialize and deserialize, easy to read


  • 0
    T
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
     
    class QueueNode{
        
        public TreeNode node;
        public boolean isLeaf;
        public QueueNode(TreeNode node, boolean isLeaf){
            this.node = node;
            this.isLeaf = isLeaf;
        }
    } 
    public class Codec {
    
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            
            if(root==null)
                return "null";
            
            Queue<QueueNode> q = new LinkedList<>();
            
            q.offer(new QueueNode(root,false));
            
            StringBuilder seri = new StringBuilder();
            
            while(!q.isEmpty()){
                
                QueueNode node = q.poll();
                
                if(!node.isLeaf){
                    
                    TreeNode tmp = node.node;
                
                    seri.append(" "+tmp.val);
                
                    if(tmp.left!=null)  q.offer(new QueueNode(tmp.left, false));
                    else                 q.offer(new QueueNode(null, true));
                    
                    if(tmp.right!=null) q.offer(new QueueNode(tmp.right, false));
                    else                 q.offer(new QueueNode(null, true));
                }
                else
                    seri.append(" null");
      
            }
            
            return seri.toString().substring(1);
            
        }
    
        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            
            if(data.equals("null"))
                return null;
            
            String[] seri = data.split(" ");
            
            TreeNode root = new TreeNode(Integer.valueOf(seri[0]));
            
            TreeNode node;
            Queue<TreeNode> q = new LinkedList<>();
            q.offer(root);
            
            for(int i=1; i<seri.length;){
                
                node = q.poll();
                
                if(seri[i].equals("null")){
                    node.left = null;
                }
                else{
                    node.left = new TreeNode(Integer.valueOf(seri[i]));
                }
                i++;
                
                
                if(seri[i].equals("null")){
                    node.right = null;
                }
                else{
                    node.right = new TreeNode(Integer.valueOf(seri[i]));
                }
                i++;
                
                if(node.left!=null)
                    q.offer(node.left);
                if(node.right!=null)
                    q.offer(node.right);
                
                
            }
            return root;
        }
    }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.deserialize(codec.serialize(root));
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.