Serialize and Deserialize Binary Tree - Problem
Serialization is the process of converting a data structure into a sequence of bits that can be stored in memory, saved to a file, or transmitted over a network. Your task is to design an algorithm that can both serialize a binary tree into a string and deserialize that string back into the original tree structure.

Think of it like creating a blueprint of a tree that you can later use to rebuild the exact same tree. You have complete creative freedom in how you represent the tree as a string - whether using preorder, level-order, or any other format.

The key challenge is ensuring that your serialization captures enough information to perfectly reconstruct the tree, including the structure and null nodes. Your serialize() function should convert a binary tree to a string, and deserialize() should convert that string back to the original tree.

Input & Output

example_1.py โ€” Complete Binary Tree
$ Input: root = [1,2,3,null,null,4,5]
โ€บ Output: Serialized: "1,2,null,null,3,4,null,null,5,null,null" (preorder) Deserialized: [1,2,3,null,null,4,5] (original tree structure)
๐Ÿ’ก Note: The tree has 5 nodes. Preorder traversal visits: 1 (root) โ†’ 2 (left) โ†’ null,null (2's children) โ†’ 3 (right) โ†’ 4,5 (3's children) โ†’ null,null,null,null (leaf nulls). The serialization captures the exact structure to enable perfect reconstruction.
example_2.py โ€” Single Node Tree
$ Input: root = [1]
โ€บ Output: Serialized: "1,null,null" Deserialized: [1]
๐Ÿ’ก Note: A single node tree serializes as the node value followed by two null markers for its missing left and right children. This minimal case demonstrates that even simple structures need null markers for complete reconstruction.
example_3.py โ€” Empty Tree Edge Case
$ Input: root = []
โ€บ Output: Serialized: "null" Deserialized: []
๐Ÿ’ก Note: An empty tree (null root) serializes to just "null" and deserializes back to null. This edge case is important for handling completely empty inputs and serves as the base case for recursive algorithms.

Visualization

Tap to expand
Tree Serialization ProcessRootLeftRightOriginal Treeโ†’Serialized String"Root,Left,null,null,Right,null,null"Preorder: Root โ†’ Left โ†’ RightNulls preserve structureReady for transmissionRootLeftRightReconstructed Treeโœ“ Perfect Match!
Understanding the Visualization
1
Create Travel Route
Follow a systematic path through the family tree, writing down each person you meet and marking empty positions
2
Transmit Instructions
Send your written route to someone at a distant location who needs to rebuild the family tree
3
Follow the Route
The recipient follows your exact path, creating each family member and connecting relationships
4
Perfect Reconstruction
The rebuilt family tree matches the original exactly, preserving all relationships and structure
Key Takeaway
๐ŸŽฏ Key Insight: Preorder traversal with null markers creates a self-contained blueprint that can rebuild any binary tree structure in a single pass, making it the most elegant solution for tree serialization.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all nodes for both serialize and deserialize operations

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h, plus O(n) for string representation

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [0, 104]
  • -1000 โ‰ค Node.val โ‰ค 1000
  • Your serialization and deserialization algorithm must be stateless
  • The serialized string should be as compact as possible while preserving structure
Asked in
Amazon 87 Google 72 Facebook 64 Microsoft 58 LinkedIn 45 Uber 38
73.2K Views
Very High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen