2Brute2Best
πŸ”₯0

Serialize and Deserialize Binary Tree

HardΒ·LeetCode #297 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

Design a way to turn a binary tree into a string, and back into the same tree.

Write a Codec class with two methods. serialize takes the root and returns a string. deserialize takes that string and returns the root of a tree with the same shape and the same values. You choose the format. Any format works, as long as the round trip gives back the same tree.

Input

For serialize: the root of a binary tree, which can be empty. For deserialize: a string made by serialize.

Output

For serialize: a string. For deserialize: the root of the rebuilt tree.

Example 1
Input: root = [1, 2, 3, null, null, 4, 5]
Output: [1, 2, 3, null, null, 4, 5]

The string can look like anything. What matters is that reading it back gives the same tree.

Example 2
Input: root = []
Output: []

An empty tree must also survive the round trip.

Constraints
  • β€’ 0 ≀ number of nodes ≀ 10^4
  • β€’ βˆ’1000 ≀ Node.val ≀ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œserialize and deserialize”
β†’ The two directions must agree on the order of the tokens. Writing and reading in the same DFS order makes that automatic.
β€œsame shape and values”
β†’ Values alone are not enough. The missing children must be recorded too.
β€œany format”
β†’ We choose the format, so we pick the one that makes reading easy: preorder with markers.
03

Approach Complexity Ladder

Brute Force β€” level order with null entries (BFS both ways)O(N)O(W)

Write the tree level by level with null for each missing child, and rebuild it with a queue

Optimal β€” preorder with # markers, read back recursivelyO(N)O(N)

Write the tree in preorder with # for each missing child; read the tokens back in the same order

Brute Force Intuition

Write the tree the way LeetCode prints it: level by level, with null for every missing child. Read it back with a queue: the next two tokens are the children of the node at the front of the queue. The null entries keep the shape, so the writer and the reader match each other step for step.

Recognition rule for this sub-pattern:

Rebuild or reshape a tree; decide the order you visit nodes so pointers are not lost.
Remember this Β· Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.