Inorder Successor in BST
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 search tree with distinct values and an integer p that is the value of one of its nodes. Return the value of the **in-order successor** of that node: the node visited immediately after it in an in-order traversal, which is the node with the smallest value strictly greater than p.
If the node holding p is the largest value in the tree, it has no successor; return -1 in that case.
Try to use the BST ordering so the search takes O(h) time, where h is the tree height.
Examples
Constraints
- 1 <= number of nodes <= 10^4
- -10^5 <= Node.val <= 10^5
- All values are distinct
- `p` is the value of a node in the 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.