Construct Binary Tree from Preorder and Inorder
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given two integer arrays, preorder and inorder, which are the preorder and inorder traversals of the same binary tree. All node values are **distinct**, so exactly one tree matches both traversals.
Rebuild that tree and return its root. The tree is graded by its level-order serialization (for example [3,9,20,null,null,15,7]).
Aim for O(n) time: the first value of preorder is always the root, and its position in inorder splits the remaining values into the left and right subtrees.
Examples
Constraints
- 1 <= preorder.length <= 3000
- inorder.length == preorder.length
- -3000 <= preorder[i], inorder[i] <= 3000
- All values are distinct
- Both arrays describe the same binary tree
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.