Serialize and Deserialize BST

Design an algorithm to serialize (convert to string) and deserialize (reconstruct from string) a Binary Search Tree (BST).

🎯 Goal: Your task is to implement two functions:
β€’ serialize(root) - Convert BST to a compact string representation
β€’ deserialize(data) - Reconstruct the original BST from the string

πŸ“¦ Key Requirements:
β€’ The encoded string should be as compact as possible
β€’ The deserialized tree must have the exact same structure as the original
β€’ No restrictions on your serialization format

πŸ’‘ What makes this special: Unlike regular binary trees, BSTs have the property that left children are smaller and right children are larger than their parent. This property can be leveraged to create more efficient serialization!

Example:
Original BST: [2,1,3] β†’ Serialize β†’ "2,1,3" β†’ Deserialize β†’ [2,1,3]

Input & Output

example_1.py β€” Basic BST
$ Input: root = [2,1,3]
β€Ί Output: serialize(root) = "2,1,3", deserialize("2,1,3") = [2,1,3]
πŸ’‘ Note: The BST with root 2, left child 1, right child 3 is serialized to the compact string "2,1,3" using preorder traversal. During deserialization, we rebuild the exact same structure using BST properties.
example_2.py β€” Single Node
$ Input: root = [1]
β€Ί Output: serialize(root) = "1", deserialize("1") = [1]
πŸ’‘ Note: A single node BST is serialized as just the node value. The deserialization creates a single node with no children, maintaining the original structure.
example_3.py β€” Empty Tree
$ Input: root = []
β€Ί Output: serialize(root) = "", deserialize("") = []
πŸ’‘ Note: An empty BST serializes to an empty string. Deserializing an empty string returns null/None, representing an empty tree.

Visualization

Tap to expand
BST Serialization Magic ✨52813Original BSTPreorder"5,2,1,3,8"🎯 Compact: No nulls needed!Bounds Check52813Rebuilt BSTπŸ’‘ Key Insight: BST property eliminates need for structural markers!
Understanding the Visualization
1
🌳 Original BST Structure
We start with a BST where left children are smaller and right children are larger than their parent
2
πŸ“ Serialize with Preorder
Walk the tree in preorder (root→left→right) and collect only the values: no nulls needed!
3
πŸ”§ Deserialize with Bounds
For each value, use min/max bounds to determine if it goes left or right
4
βœ… Perfect Reconstruction
The BST property ensures we rebuild the exact same structure
Key Takeaway
🎯 Key Insight: The BST ordering property is so powerful that we can reconstruct the entire tree structure from just the node values in preorder sequence, making this the most space-efficient serialization possible!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Visit each node once for serialization and deserialization

n
2n
βœ“ Linear Growth
Space Complexity
O(n)

Store all nodes including nulls, queue for BFS

n
2n
⚑ Linearithmic Space

Constraints

  • The number of nodes in the tree is in the range [0, 104]
  • 0 ≀ Node.val ≀ 104
  • The input tree is guaranteed to be a valid binary search tree
  • Your serialized string should be as compact as possible
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28 Apple 22
125.0K Views
High Frequency
~25 min Avg. Time
2.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