Clone binary tree from different machine
Filed under Amazon
Free read 3 of 3Determine the serialization format used to transmit the tree data from the remote machine.
The question
Reconstruct or clone a binary tree whose nodes are serialized and sent from a different machine, handling pointer reconstruction across serialized boundaries.
Constraints
- The tree may be unbalanced or skewed.
- Serialization format must handle missing children (e.g., null markers).
Examples
- Input
- Serialized array: [1, 2, 3, null, null, 4, 5]
- Output
- Root node of the reconstructed binary tree
- Why
- Standard level-order or pre-order serialization with null markers allows exact tree reconstruction.
Approach
Determine the serialization format used to transmit the tree data from the remote machine.
Build the tree level by level using an adjacency list representation sent over the wire.
Use pre-order traversal with a queue or iterator to deserialize nodes recursively in O(N) time.
Corrupted transmission stream, empty data streams, and extremely deep trees causing recursion limits.
Tips
- What they are testing: Test with an empty tree, single node, and highly unbalanced trees.
- Common mistake: Failing to correctly account for null children during deserialization, leading to misaligned pointer mapping.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.