Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.

There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.

Example

An example of testdata: Binary tree{3,9,20,#,#,15,7}, denote the following structure:

3
 / \
9  20
  /  \
 15   7

BFS approach.

  1. Serialization

    use arraylist or any data structure that supports the null values, we need to get rid of the trailing null values as they should    
    
       be excluded
    

    2.Deserialization

    use arraylist as the nodes list and ans list. ans list keeps the nodes that got created when going through the string array, we basically create new node from nodes and have it link back to the index of ans list.

/**
 * 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;
 *     }
 * }
 */
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
        String ans = "";
        if (root == null) {
            return ans;
        }

        ArrayList<TreeNode> queue = new ArrayList<>();
        queue.add(root);
        for (int i = 0; i < queue.size(); i++) {
            TreeNode cur = queue.get(i);

            if (cur == null) {
                continue;
            }
            queue.add(cur.left);
            queue.add(cur.right);
        }

        while (queue.get(queue.size() - 1) == null) {
            queue.remove(queue.size() - 1);
        }


        ans = Integer.toString(queue.get(0).val);
        for (int i = 1; i < queue.size(); i++) {
            String newString = "";
            if (queue.get(i) == null) {
                newString = "#";
            } else {
                newString = Integer.toString(queue.get(i).val);
            }
            ans = ans + "," + newString;
        }

        return ans;

    }

    /**
     * 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 == null || data.length() == 0) {
            return null;
        }

        String[] queue = data.split(",");
        ArrayList<TreeNode> ans = new ArrayList<>();
        TreeNode root = new TreeNode(Integer.parseInt(queue[0]));
        ans.add(root);


        int index = 0;
        boolean isLeft = true;

        for (int i = 1; i < queue.length; i++) {
            if (!queue[i].equals("#")) {
                TreeNode cur = new TreeNode(Integer.parseInt(queue[i]));
                if (isLeft) {
                    ans.get(index).left = cur;
                } else {
                    ans.get(index).right = cur;
                }
                ans.add(cur);
            }

            if (!isLeft) {
                index++;
            }
            isLeft = !isLeft;
        }

        return root;



    }


}

results matching ""

    No results matching ""