Serialize and deserialize a binary tree
Hard TimeO(n) SpaceO(n)
Write a binary tree out as a string, and read that string back into an identical tree — identical in shape as well as in values, because several different trees hold the same values in the same order. Empty children have to be represented too, or the shape is lost, and an empty tree has to survive the round trip as well.
Examples
Example 1
- Input
- roundTrip()
- Output
- The tree written out, read back, and written out again — the two strings match exactly.
["4,2,1,#,#,3,#,#,7,6,#,#,9,#,#","4,2,1,#,#,3,#,#,7,6,#,#,9,#,#"]
Example 2
- Input
- node = buildTree().left
- Output
"2,1,#,#,3,#,#"2, then its left1with two missing children, then its right3with two more.
The Code
function buildTree() {
return {
val: 4,
left: { val: 2, left: { val: 1, left: null, right: null }, right: { val: 3, left: null, right: null } },
right: { val: 7, left: { val: 6, left: null, right: null }, right: { val: 9, left: null, right: null } }
};
}
function serialize(node) {
if (node === null) return "#";
return node.val + "," + serialize(node.left) + "," + serialize(node.right);
}
function deserialize(data) {
const tokens = data.split(",");
let index = 0;
function build() {
const token = tokens[index];
index++;
if (token === "#") return null;
const node = { val: Number(token), left: null, right: null };
node.left = build();
node.right = build();
return node;
}
return build();
}
function roundTrip() {
const text = serialize(buildTree());
return [text, serialize(deserialize(text))];
}
roundTrip();Done
The first 10 calls, of 48. This one does not fit on a page.
More like this
All binary trees examples (9) →- Inorder traversal Left subtree, then the node, then right — a BST comes out sorted.
- Max tree depth A node is one deeper than its deepest child.
- Invert a tree Swap every node’s children, all the way down.
- Validate a BST Every node needs a valid range, not just a valid parent.
- Level order (BFS) Process a whole row, collecting the next one as you go.
- Lowest common ancestor Walk down until the two targets fall on opposite sides.