Description
Design an algorithm to serialize and deserialize a binary search tree. Serialization is converting to string, deserialization is reconstructing the tree. Return the result as an array.
Examples
Input:
root = [2,1,3]Output:
[2,1,3]Explanation:
BST with root=2, left=1, right=3. Serialize to string '2,1,3'. Deserialize: 2 becomes root, 1<2 goes left, 3>2 goes right. Tree structure [2,1,3] is preserved.
Input:
root = [1]Output:
[1]Explanation:
Single node BST with value 1. Serialize to '1'. Deserialize creates root node with value 1. No children to process.
Input:
root = [5,2,7,1,4,6,9]Output:
[5,2,7,1,4,6,9]Explanation:
BST with root=5: left subtree [2,1,4] (2 as root, 1 left, 4 right), right subtree [7,6,9] (7 as root, 6 left, 9 right). Serialize using preorder: '5,2,1,4,7,6,9'. Deserialize by using BST property to place each value correctly.
Constraints
- •
Number of nodes is in range [0, 10⁴] - •
0 ≤ Node.val ≤ 10⁴