Hard · Trees
Serialize and Deserialize Binary Tree
Design a Codec for binary trees. serialize(root) turns a tree into a string, and deserialize(data) rebuilds a tree from such a string, so that deserialize(serialize(root)) has exactly the same shape and values as root. Any string format is allowed; the reference writes a preorder walk with "null" for every missing child.
Examples
Example 1
[1,2,3,null,null,4,5]Output: round-trip 5/5 nodes
Example 2
[5,3,8,1,4]Output: round-trip 5/5 nodes
Rebuild it in the studio
Open every published interview problem, solution, scene, and memory room free, with no account.