LC 297
LeetCode 297 · Hard

Serialize and Deserialize Binary Tree

Encode a binary tree to a string and decode it back.

BFS → · High frequency · Solve on LeetCode →

Try it

Step through the core mechanic. The simulator below runs the bfs shape this problem is built on.

Walk the pattern

No dedicated step-through for this one yet. The shape is BFS — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.

The approach

Pre-order DFS (or BFS) writing null markers so structure is recoverable. Deserialize by consuming tokens in the same order, rebuilding nodes and recursing on the markers.

AspectValue
PatternBFS
Recognise it byRound-trip a tree through a string.
Time complexityO(n)
Space complexityO(n)
DifficultyHard

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.