Serialize and Deserialize Binary Tree
Design an algorithm to serialize a binary tree to a single string and deserialize that string back into the identical tree structure. You implement two methods, serialize and deserialize, and the round trip must reproduce the original tree.
Open official problem prompt ↗Convert a binary tree into a reversible string and reconstruct the exact same tree from that string, preserving both values and structure.
Think of dictating a family tree over the phone. If you only read names you lose who is missing; but if you also say 'no child here' at every gap, the listener can redraw the tree exactly. The '#' marker is that spoken 'no child here'.
- Input
- root = [1, 2, 3, null, null, 4, 5]
- Output
- [1, 2, 3, null, null, 4, 5]
- Why
- serialize produces a string encoding the tree, and deserialize rebuilds the exact same tree, so the round trip returns the original.
The number of nodes is in the range [0, 10^4]-1000 <= Node.val <= 1000