Serialize and Deserialize BST - Problem
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:
β’
β’
π¦ 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]
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
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
β Linear Growth
Space Complexity
O(n)
Store all nodes including nulls, queue for BFS
β‘ 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
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code