0
0
DSA Typescriptprogramming

Serialize and Deserialize Binary Tree in DSA Typescript

Choose your learning style9 modes available
Mental Model
We turn a tree into a string to save or send it, then rebuild the exact same tree from that string.
Analogy: Like writing a family tree on paper and later using that paper to redraw the same family tree exactly as it was.
    1
   / \
  2   3
     / \
    4   5

Serialize as: "1,2,null,null,3,4,null,null,5,null,null,"
Dry Run Walkthrough
Input: Binary tree: 1 with left child 2 and right child 3; 3 has children 4 and 5
Goal: Convert the tree into a string and then rebuild the same tree from that string
Step 1: Start serialization at root node 1
Serialize: "1,"
Why: We record the root value first to start the string
Step 2: Serialize left subtree of 1 (node 2)
Serialize: "1,2,"
Why: We record left child value next
Step 3: Serialize left child of 2 which is null
Serialize: "1,2,null,"
Why: We mark null to remember no left child
Step 4: Serialize right child of 2 which is null
Serialize: "1,2,null,null,"
Why: We mark null to remember no right child
Step 5: Serialize right subtree of 1 (node 3)
Serialize: "1,2,null,null,3,"
Why: We record right child value after left subtree
Step 6: Serialize left child of 3 (node 4)
Serialize: "1,2,null,null,3,4,"
Why: We record left child of 3
Step 7: Serialize left and right children of 4 which are null
Serialize: "1,2,null,null,3,4,null,null,"
Why: Mark nulls for 4's children
Step 8: Serialize right child of 3 (node 5) and its null children
Serialize: "1,2,null,null,3,4,null,null,5,null,null,"
Why: Complete serialization with right subtree
Step 9: Start deserialization from string
Deserialize: reading "1" creates root node 1
Why: We rebuild root first
Step 10: Deserialize left subtree from next tokens "2,null,null"
Deserialize: node 2 with null children attached to root
Why: Rebuild left subtree exactly
Step 11: Deserialize right subtree from tokens "3,4,null,null,5,null,null"
Deserialize: node 3 with children 4 and 5 rebuilt
Why: Rebuild right subtree exactly
Result:
Final tree rebuilt exactly as original:
    1
   / \
  2   3
     / \
    4   5
Annotated Code
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

class Codec {
  // Serialize tree to string
  serialize(root: TreeNode | null): string {
    if (root === null) return "null,"; // mark null nodes
    return root.val + "," + this.serialize(root.left) + this.serialize(root.right);
  }

  // Deserialize string to tree
  deserialize(data: string): TreeNode | null {
    const nodes = data.split(",");
    function helper(): TreeNode | null {
      const val = nodes.shift();
      if (val === "null" || val === undefined || val === "") return null; // null node found or empty string
      const node = new TreeNode(parseInt(val));
      node.left = helper(); // build left subtree
      node.right = helper(); // build right subtree
      return node;
    }
    return helper();
  }
}

// Driver code
const root = new TreeNode(1, new TreeNode(2), new TreeNode(3, new TreeNode(4), new TreeNode(5)));
const codec = new Codec();
const serialized = codec.serialize(root);
console.log("Serialized:", serialized);
const deserializedRoot = codec.deserialize(serialized);

// Helper to print tree inorder for verification
function inorderPrint(node: TreeNode | null): string {
  if (!node) return "null";
  return node.val + "(" + inorderPrint(node.left) + "," + inorderPrint(node.right) + ")";
}
console.log("Deserialized inorder:", inorderPrint(deserializedRoot));
if (root === null) return "null,";
mark null nodes explicitly to preserve tree shape
return root.val + "," + this.serialize(root.left) + this.serialize(root.right);
serialize current node then left and right subtrees recursively
const val = nodes.shift();
read next value from serialized data to rebuild node
if (val === "null" || val === undefined) return null;
detect null marker to stop recursion on this branch
node.left = helper();
rebuild left subtree recursively
node.right = helper();
rebuild right subtree recursively
OutputSuccess
Serialized: 1,2,null,null,3,4,null,null,5,null,null, Deserialized inorder: 1(2(null,null),3(4(null,null),5(null,null)))
Complexity Analysis
Time: O(n) because we visit each node once during serialization and once during deserialization
Space: O(n) for the recursion stack and the serialized string storing all nodes
vs Alternative: Compared to level-order serialization, preorder with null markers is simpler and uses recursion, but both have similar O(n) cost
Edge Cases
Empty tree (root is null)
Serialization returns "null," and deserialization returns null tree
DSA Typescript
if (root === null) return "null,";
Tree with only one node
Serialization returns "val,null,null," and deserialization rebuilds single node
DSA Typescript
if (val === "null" || val === undefined) return null;
Tree with only left or only right children
Null markers preserve missing children so tree shape is exact
DSA Typescript
if (val === "null" || val === undefined) return null;
When to Use This Pattern
When you need to save or send a tree structure and later rebuild it exactly, use serialize/deserialize with preorder traversal and null markers to preserve shape.
Common Mistakes
Mistake: Not marking null children during serialization, causing incorrect tree shape on deserialization
Fix: Always serialize null children explicitly as "null," to keep tree structure
Mistake: Using split without handling trailing commas causing empty strings in array
Fix: Ensure to handle or ignore empty strings after splitting serialized data
Summary
It converts a binary tree to a string and back to the same tree.
Use it when you want to save or transfer a tree and restore it later exactly.
The key is to record null children explicitly to keep the tree shape intact.