Encode a binary tree into a string. Decode it back. This sounds like a systems problem — marshalling and unmarshalling. But underneath the practical framing is one of the most beautiful questions in computer science: what information does a tree contain, and how much of that information do you need to reconstruct it perfectly?
Design two functions — serialize(tree) returns a string, and deserialize(string) returns a tree — such that deserialize(serialize(tree)) always reconstructs the original tree exactly. The round-trip property is the contract.
"1,2,N,N,3,4,N,N,5,N,N"
The obvious encodings lose information. Inorder values alone are ambiguous — multiple trees produce the same sequence. Preorder values alone are ambiguous. Level-order without null markers is ambiguous. The encoding has to capture STRUCTURE alongside values, and the round-trip property is the contract that proves it worked.
Suppose I serialize this tree using just the inorder values: 21435. Can deserialize reconstruct the original tree from this string alone?