Problem
Design serialize() to encode a binary tree as a single string, and deserialize() to rebuild the identical tree from that string. The two must be inverses: deserialize(serialize(root)) reproduces the original structure and values.
Input / Output
- Input: the root of a binary tree (values may repeat and may be negative).
- Output: serialize -> a string; deserialize -> the reconstructed root.
Constraints
- Up to 10^4 nodes.
- Must handle null children and the empty tree.
- Structure, not just the multiset of values, must be preserved.
Example
- [1,2,3,null,null,4,5] -> some string -> a tree structurally equal to the original.