Practice/LinkedIn/Leetcode 449. Serialize and Deserialize BST
CodingMust
Create two methods for a Codec class that can convert a Binary Search Tree into a compact string representation and restore it back to the original tree structure.
Your implementation should:
serialize(root): Convert a BST into a string formatdeserialize(data): Reconstruct the BST from the string formatThe key challenge is to leverage the BST property (left subtree values < node value < right subtree values) to create an efficient encoding that doesn't require storing null markers or explicit structural information beyond the node values themselves.
serialize to convert a BST to a string representationdeserialize to reconstruct the exact original BST from the stringdeserialize(serialize(tree)) produces a tree identical to the originalExample 1:
`
Input: root = [2,1,3]
2
/
1 3
serialize(root) = "2,1,3" deserialize("2,1,3") reconstructs the same tree `
Example 2:
` Input: root = [5] 5
serialize(root) = "5" deserialize("5") reconstructs a single-node tree `
Example 3:
Input: root = [] serialize(root) = "" deserialize("") returns null
Example 4:
`
Input: root = [10,5,15,2,7,null,20]
10
/
5 15
/ \
2 7 20
serialize(root) = "10,5,2,7,15,20" (preorder traversal is sufficient due to BST property) `