Serialize and Deserialize Binary Tree
Problem
Design a way to turn a binary tree into a string (serialize) and later rebuild an equivalent tree from that string (deserialize), so the round trip reproduces the tree's exact structure and node values.
Example. For a tree with root 1, right child 2, whose own children are 3 and 4, a serialization might read as the sequence 1, null, 2, 3, 4, recording both the values present and where a child is missing.
Key idea
Storing only node values, say from an inorder traversal, is not enough: many different tree shapes can share the same values in the same order, so shape is lost. The fix is to record a placeholder wherever a child pointer is empty, not just the nodes that exist.
A preorder traversal does this cleanly: write the node's value, then recurse left, then right, writing a marker such as "null" whenever a child is absent. Because preorder visits a node before its children, and every missing child is recorded, the string uniquely determines one tree. Deserializing replays the same logic: read one token at a time; if it is the null marker, that position stays empty, otherwise create a node and fill in its left then right child from the tokens that follow.
Solution
Complexity
- Time: O(n). Serialization and deserialization each write or read one token per node and per null pointer, exactly once.
- Space: O(n). The output string, plus O(h) for the recursion stack.
Watch out for
- Omitting null markers reintroduces ambiguity:
1, 2, 3cannot say whether2is1's left or right child. - Choose a delimiter that cannot appear inside a value's own text representation, or token boundaries become ambiguous.
- Deserialization must consume tokens in the exact order produced, using a shared index across recursive calls rather than re-scanning from the start each time.
Pattern
This is "preorder traversal with explicit null markers," a general way to flatten a pointer-based tree into an order-preserving string, and it extends to n-ary trees by recording each node's child count.