DSA Tracker

Problem

Hard

Serialize and Deserialize Binary Tree

A hard Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
2
Core for
4 roles

The problem

Serialize and deserialize a binary tree. Serialization converts a tree into a string representation, and deserialization reconstructs the original tree from that string. The algorithm must work for any valid binary tree.

Example 1

Input
serialize([1,2,3,null,null,4,5])
Output
deserialize('1,2,3,null,null,4,5') returns [1,2,3,null,null,4,5]
Why
The tree is encoded into a comma-separated level-order string and reconstructed exactly.

Example 2

Input
serialize([])
Output
deserialize('') returns []
Why
An empty tree serializes to an empty string.

Example 3

Input
serialize([1])
Output
deserialize('1') returns [1]
Why
A single node serializes and deserializes correctly.

Constraints

  • The number of nodes is in the range [0, 104].
  • -1000 <= Node.val <= 1000

How to think about it

Updated 2026-09-29

Standard traversals cannot reconstruct a tree without both inorder and preorder. However, if you explicitly record null children as unique markers (like `#`), preorder or BFS becomes unambiguous and self-delimiting, enabling exact reconstruction from a single serialized token stream.

Approaches, worst first

  1. BFS level-order serialization

    time O(n) · space O(n)

    Traverse level by level using a queue, recording values or `#` for nulls separated by commas. Deserialization splits tokens into an array, using a pointer and a queue to attach pairs of children to parents in sequence.

  2. DFS preorder recursionWrite this one

    time O(n) · space O(n)

    Preorder DFS writes `val,` for nodes and `#,` for nulls. Deserialization parses the token stream with an iterator: if token is `#`, return null; otherwise make a node and recursively assign its left and right children.

Where people lose marks · 3
  • Omitting clear delimiters between node values allows multi-digit numbers and negative signs to blend together (e.g. 1, -23 vs 1-, 2, 3).
  • Handling an empty tree improperly; serializing empty tree as `''` must return null during deserialization rather than a node with NaN value.
  • Using array `.shift()` repeatedly during token deserialization turns parsing quadratic; maintain a moving index pointer instead.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .