pbPassingBI
/

Explain and implement a serialization of a binary tree.

advanced
Answer

This is a general programming question rather than a Tableau one, and it turns up in data roles where some engineering is expected.

Serialising a binary tree means converting it to a string that can be stored or transmitted, in a form that can be turned back into the identical tree.

The straightforward approach is a pre-order traversal that records nulls explicitly. Visit the node, write its value, then recurse left and right; where a child is absent, write a marker such as `#`. A tree with root 1, left 2 and right 3 becomes `1,2,#,#,3,#,#`.

Deserialising reads the tokens in the same order: take the next token, and if it is the marker return null, otherwise create the node and recursively build its left then right child. Both operations are O(n) in time and space.

Recording the nulls is what makes it work — without them the structure is ambiguous, since a pre-order sequence alone does not distinguish trees of different shapes.

Related questions