Skip to main content

Serialize and Deserialize Binary Tree


Serialize and Deserialize Binary Tree: 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.

Example 1:

image

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

Example 2:
Input: root = []
Output: []

Constraints:
  • The number of nodes in the tree is in the range [1, 3 * 10^4].
  • -1000 <= Node.val <= 1000

Try this Problem on your own or check similar problems:

  1. Serialize and Deserialize BST
  2. Find Duplicate Subtrees
Solution:
/**
* 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 sb = new StringBuilder();
buildSerialization(root, sb);
return sb.toString();
}

private void buildSerialization(TreeNode root, StringBuilder sb){
if(root == null){
sb.append("X").append(",");
return;
}
sb.append(root.val).append(",");
buildSerialization(root.left, sb);
buildSerialization(root.right, sb);
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == "") return null;
Queue<String> q = new LinkedList<>(Arrays.asList(data.split(",")));
return buildTree(q);
}

private TreeNode buildTree(Queue<String> q){
String val = q.poll();
if(val.equals("X")) return null;
TreeNode root = new TreeNode(Integer.valueOf(val));
root.left = buildTree(q);
root.right = buildTree(q);
return root;
}
}

Time/Space Complexity:
  • Time Complexity: O(n)
  • Space Complexity: O(n)

Explanation:

We use StringBuilder and Queue for serialize and deserialize which leads us to linear space complexity, we traverse all nodes in the tree (and all characters in serialization) leading to linear time complexity too. In the Construct Binary Tree from Preorder and Inorder Traversal we needed both inorder and preorder traversals to build the tree. Note that we needed preorder to determine what is the root of the current subtree, and we needed inorder to determine the left and right subtree size for each node. But in this problem, we're not bounded by already defined formats of traversals. So we could utilize the preorder but this time also marking the end of each node's left and right subtree with 'X' which leads to 1,2,X,X,3,4,X,X,5,X,X, preorder sequence for the given example, so for serialize we do a preorder on the tree and build the string with X as markers for end of substrees. For deserialize we use a queue so we can keep track of the current val in the serialization, and each time we hit 'X' we know the subtree ends there and we return null. We recursively build the left and right subtree.