Serialize and Deserialize Binary Tree
Design a pair of functions: serialize converts a binary tree into a single string, and deserialize converts that string back into a tree identical to the original. You may pick any string format you like — the only requirement is that a round trip through both functions reproduces the exact tree.
The tree has 0 to 10,000 nodes with values between -1000 and 1000. Values can be negative and multi-digit, so a delimiter between them is essential.
Examples
- Input:
- root = [1,2,3,null,null,4,5]
- Output:
- deserialize(serialize(root)) rebuilds [1,2,3,null,null,4,5]
One possible encoding: the preorder string "1,2,N,N,3,4,N,N,5,N,N" where N marks an empty child.
- Input:
- root = []
- Output:
- deserialize(serialize(root)) rebuilds []
The empty tree encodes as the single marker "N".
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.