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.

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

Visualization

Tap to expand
Serialize and Deserialize Binary Tree INPUT Binary Tree Structure 1 2 3 4 5 null null root = [1,2,3,null,null,4,5] Level-order representation DFS Preorder Approach Visit: Root --> Left --> Right ALGORITHM STEPS 1 Serialize (DFS) If null, add "null" to result 2 Process Node Add value, recurse L/R 3 Deserialize Split string by comma 4 Rebuild Tree Create nodes recursively Preorder Traversal: 1 --> 2 --> null --> null --> 3 --> 4 --> null --> null --> 5 --> null --> null Stack tracks parent nodes for reconstruction FINAL RESULT Serialized String: "1,2,null,null,3,4, null,null,5,null,null" (Preorder format) Deserialized Tree: 1 2 3 4 5 OK - Tree Restored! [1,2,3,null,null,4,5] Key Insight: Preorder DFS naturally captures tree structure. By marking null nodes explicitly, we can perfectly reconstruct the tree without needing level information. Time Complexity: O(n) for both serialize and deserialize. Space: O(n) for output string. TutorialsPoint - Serialize and Deserialize Binary Tree | DFS Approach
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