KatInterview

Assemble a Binary Tree from Two Traversals, Output by Level

Trees & GraphsAuto-graded · 22 test cases
MediumAuto-gradedTrees & Graphsbinary-treetraversalBFS

Last updated

Tests whether a candidate can reconstruct a unique binary tree from its preorder and inorder sequences and serialize the result breadth-first with null markers.

You receive two integer arrays, pre and ino, representing the preorder and inorder traversals of the same binary tree. Every value in the tree is unique. Rebuild the tree and return it as a level-order list with None filling any gap where a child is absent.

Traversal Definitions

  • Preorder: record the current node, recurse into the left subtree, then recurse into the right subtree.
  • Inorder: recurse into the left subtree, record the current node, then recurse into the right subtree.

Because all values are distinct, the two traversals together describe exactly one tree, so your output is always unique.

How to Serialize the Result

Use a queue-based level-order scan:

  1. Seed the queue with the root node.
  2. On each step, remove the front element.
    • If it is a real node, write its value to the output list, then push its left child and its right child onto the queue. Push a None placeholder for any child that does not exist.
    • If it is a None placeholder, write None to the output list and push nothing.
  3. Repeat until the queue is empty.
  4. Remove every None value from the end of the output list before returning. None values that appear in the middle must stay, because they encode the tree's shape.

Constraints

  • 1 <= len(pre) == len(ino) <= 3000
  • -10^9 <= pre[i] <= 10^9; all values are distinct integers.
  • ino contains exactly the same values as pre, and both arrays are guaranteed to be valid traversals of one binary tree.
  • The tree may be fully skewed, reaching a depth equal to the total number of nodes.

Examples

Example 1

Input: assemble_tree([1, 2, 4, 5, 3, 6], [4, 2, 5, 1, 3, 6])

Output: [1, 2, 3, 4, 5, None, 6]

Example 2

Input: assemble_tree([10, -2, 30], [10, 30, -2])

Output: [10, None, -2, 30]

Example 3

Input: assemble_tree([7], [7])

Output: [7]

Hints

Hint 1

The first element of the preorder array is always the root. Once you know the root, its position in the inorder array divides everything to its left into the left subtree and everything to its right into the right subtree. Build a hash map of value → inorder index up front so each lookup is O(1).

Hint 2

An iterative approach with an explicit stack avoids hitting Python's default recursion limit on a fully skewed 3000-node tree. The stack holds nodes on the current path from the root whose right child has not yet been assigned. When the next preorder value has a smaller inorder index than the stack top, it becomes that top node's left child; otherwise, pop nodes until you find the parent of the ne…

Hint 3

For the serialization pass, use a collections.deque and push explicit None sentinels for missing children — but only for real nodes, never for a None sentinel itself. After the BFS finishes, strip trailing Nones. Be careful with node values like 0: always check `x is None`, never rely on truthiness.