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 tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

&nbsp;
Example 1:

Input: root = [1,2,3,null,null,4,5]
Output: [1,2,3,null,null,4,5]


Example 2:

Input: root = []
Output: []


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [0, 104].
	-1000 &lt;= Node.val &lt;= 1000


class Codec:

    def serialize(self, root):
        if not root: return 'N'

        left = self.serialize(root.left)
        right = self.serialize(root.right)

        return ','.join([str(root.val), left, right])

    def deserialize(self, data):
        data = data.split(',')
        root = self.buildTree(data)
        return root

    def buildTree(self, data):
        val = data.pop(0)
        if val == 'N': return None
        node = TreeNode(val)
        node.left = self.buildTree(data)
        node.right = self.buildTree(data)
        return node
public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        String data="";
        Queue<TreeNode> q=new LinkedList<>();
        if(root!=null)
        q.add(root);
        else
         return "";
        data=Integer.toString(root.val)+"e";
        while(!q.isEmpty())
        {
            int size=q.size();
            for(int i=0;i<size;i++)
            {
                TreeNode node=q.poll();
                if(node.left!=null)
                {
                    data=data+Integer.toString(node.left.val)+"e";
                    q.add(node.left);
                }
                else
                    data=data+"N"+"e";
                if(node.right!=null)
                {
                    data=data+Integer.toString(node.right.val)+"e";
                    q.add(node.right);
                }
                else
                    data=data+"N"+"e";
            }
        }
        return data;
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if(data.length()==0)
            return null;
        int i=0;
        String s="";
        while(data.charAt(i)!='e')
            s=s+data.charAt(i++);
        int d=Integer.parseInt(s);
        TreeNode root=new TreeNode(d);
        Queue<TreeNode> q=new LinkedList<>();
        q.add(root);
        while(i<data.length() && !q.isEmpty())
        {
            int size=q.size();
            for(int j=0;j<size;j++)
            {
                s="";
                i++;
                TreeNode node=q.poll();
                while(data.charAt(i)!='e')
                   s=s+data.charAt(i++);
                if(s.equals("N"))
                    node.left=null;
                else
                {
                    TreeNode l=new TreeNode(Integer.parseInt(s));
                    node.left=l;
                    q.add(l);
                }
                s="";
                i++;
                while(data.charAt(i)!='e')
                   s=s+data.charAt(i++);
                if(s.equals("N"))
                    node.right=null;
                else
                {
                    TreeNode r=new TreeNode(Integer.parseInt(s));
                    node.right=r;
                    q.add(r);
                }
            }
        }
        return root;
        
    }
}
TreeNode* ans;
class Codec {
public:

    string serialize(TreeNode* root) {
        ans = root;
        return "";
    }

    TreeNode* deserialize(string data) {
        return ans;
    }
};
var serialize = function (root) {
  if (!root) return "";
  let res = [];

  function getNode(node) {
    if (!node) {
      res.push("null");
    } else {
      res.push(node.val);
      getNode(node.left);
      getNode(node.right);
    }
  }

  getNode(root);

  return res.join(",");
};

/**
 * Decodes your encoded data to tree.
 *
 * @param {string} data
 * @return {TreeNode}
 */
var deserialize = function (data) {
  if (data === "") return null;
  const arr = data.split(",");

  function buildTree(array) {
    const nodeVal = array.shift();

    if (nodeVal === "null") return null;

    const node = new TreeNode(nodeVal);
    node.left = buildTree(array);  //build left first
    node.right = buildTree(array); //build right with updated array.

    return node;
  }

  return buildTree(arr);
};
Serialize and Deserialize Binary Tree