Recover a Tree From Preorder Traversal
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
A binary tree was written out as a string by a pre-order traversal. For each node visited, the writer emitted D dashes, where D is the node's depth (the root has depth 0), followed immediately by the node's value.
Whenever a node has exactly one child, that child is its **left** child. Given the string traversal, rebuild the tree and return its root.
Values are positive integers and may have several digits, so read full numbers rather than single characters.
Examples
Constraints
- 1 <= number of nodes <= 1000
- 1 <= Node.val <= 10^9
- `traversal` is a valid encoding of a tree in which single children are always left children
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.