Height of Binary Tree After Subtree Removal Queries
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 the root of a binary tree with n nodes whose values are exactly 1 through n, and an array queries. Each query is independent: for queries[i], imagine deleting the subtree rooted at the node with that value (the node and all its descendants), and report the height of the remaining tree. The tree is restored before the next query.
Height is measured in **edges** on the longest path from the root down to any remaining node. No query names the root, so the root always remains.
Return an array where the i-th element answers queries[i]. Recomputing the height for each query is O(n * m); aim for O(n + m) with precomputation.
Examples
Constraints
- 2 <= n <= 10^5
- Node values are exactly `1` through `n`, each used once
- 1 <= queries.length <= min(n, 10^4)
- 1 <= queries[i] <= n
- queries[i] is never the root value
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.