Serialize and Deserialize Binary Tree
Difficulty: Hard
Design a way to turn a binary tree into a string (serialize) and to rebuild the exact same tree from it (deserialize). The round trip must keep the tree's shape, not just its values, so you need to encode where the null children are.
Any string format works, as long as deserialize(serialize(root)) always gives an equivalent tree.
Examples
Input: root = [1,2,3,null,null,4,5]
Output: deserialize(serialize(root)) reproduces [1,2,3,null,null,4,5]
The exact string format is up to you — what matters is that the shape and values survive the round trip.
Input: root = []
Output: deserialize(serialize(root)) reproduces []
An empty tree must round-trip to an empty tree too.
Constraints
The number of nodes is in the range [0, 10^4].
Node values are in the range [-1000, 1000].
Approach
The key trick is writing null children as real entries instead of leaving them out. That's what makes the shape recoverable.
Two traversals work: level-order (BFS) or preorder (DFS), each recording empty child slots. Deserialize replays the same order to rebuild the structure one node (or empty slot) at a time.
Solutions
Level-Order (BFS) with Null Markers
Serialize with a breadth-first walk using a queue. For each dequeued node, record its value (or "null" for an empty slot). If it isn't empty, enqueue both children (which may be null).
Deserialize in reverse: the first value is the root. Read the rest in order, using a queue of nodes still waiting for their two children to know where each value goes.
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function serialize(root) {
if (root === null) return "null";
const values = [];
const queue = [root];
while (queue.length > 0) {
const node = queue.shift();
if (node === null) {
values.push("null");
} else {
values.push(String(node.val));
queue.push(node.left);
queue.push(node.right);
}
}
return values.join(",");
}
function deserialize(data) {
const values = data.split(",");
if (values[0] === "null") return null;
const root = new TreeNode(Number(values[0]));
const queue = [root];
let index = 1;
while (queue.length > 0) {
const node = queue.shift();
const leftVal = values[index++];
if (leftVal !== "null") {
node.left = new TreeNode(Number(leftVal));
queue.push(node.left);
}
const rightVal = values[index++];
if (rightVal !== "null") {
node.right = new TreeNode(Number(rightVal));
queue.push(node.right);
}
}
return root;
}Time: O(n) for both serialize and deserialize · Space: O(n) — the output string and the queue
Preorder (DFS) with Null Markers
Serialize with a preorder traversal (node, then left, then right), writing "null" for each missing child. Because every missing child gets a marker, the sequence describes the shape exactly.
Deserialize by reading the sequence in order with one shared pointer. The next token is either "null" (empty subtree) or a value: build a node, then read its left subtree, then its right.
This is just as efficient as BFS, and some find it more direct, since serialize and deserialize mirror each other line for line.
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function serialize(root) {
const values = [];
function dfs(node) {
if (node === null) {
values.push("null");
return;
}
values.push(String(node.val));
dfs(node.left);
dfs(node.right);
}
dfs(root);
return values.join(",");
}
function deserialize(data) {
const values = data.split(",");
let index = 0;
function dfs() {
const val = values[index];
index++;
if (val === "null") return null;
const node = new TreeNode(Number(val));
node.left = dfs();
node.right = dfs();
return node;
}
return dfs();
}Time: O(n) for both serialize and deserialize · Space: O(n) — the output string, plus O(h) recursion stack