EvalFrame Technical Mock Interviews & Coding Practice

Construct Binary Tree from Preorder and Inorder Traversal

Medium · Binary Tree · Algorithm problem · all problems

Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return the level-order array representation of the binary tree. **Examples:** ```typescript buildTree([3,9,20,15,7], [9,3,15,20,7]) // [3, 9, 20, null, null, 15, 7] buildTree([-1], [-1]) // [-1] ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(n)
  • Space complexity: O(n)

Each node is constructed once, and map lookups for inorder index take O(1).

Follow-Up Questions Interviewers Ask About Construct Binary Tree from Preorder and Inorder Traversal

  • Why does preorder identify the root while inorder splits left and right subtrees?
    Preorder visits root first, so preorder[0] is the tree root. Inorder visits left, then root, then right, so root divides left and right subtrees.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.