Serialize and Deserialize BST

Medium

Description

Design an algorithm to serialize and deserialize a binary search tree. Serialization is converting to string, deserialization is reconstructing the tree.

Examples

Input:root = [2,1,3]
Output:[2,1,3]
Explanation:

Serialize then deserialize returns same tree.

Input:root = [1]
Output:[1]
Explanation:

Edge case with a single-element array.

Input:root = [5,2,7,1,4,6,9]
Output:[5,2,7,1,4,6,9]
Explanation:

A complete balanced BST with multiple levels. After serialization (converting to string) and deserialization (reconstructing from string), the tree structure and values are preserved exactly, demonstrating the algorithm works for larger, more complex trees.

Constraints

  • Number of nodes is in range [0, 10⁴]
  • 0 ≤ Node.val ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!