LeetCode 297 ·
Hard
Serialize and Deserialize Binary Tree
Encode a binary tree to a string and decode it back.
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.
| Aspect | Value |
|---|---|
| Pattern | BFS |
| Recognise it by | Round-trip a tree through a string. |
| Time complexity | O(n) |
| Space complexity | O(n) |
| Difficulty | Hard |
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.