Two Sum in a BST
EasyTreestwo pointers
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 unique values and an integer k. Return true if two **different** nodes have values that add up to exactly k, and false otherwise.
A single node may not be paired with itself, so a tree containing only 5 does not reach k = 10.
Examples
Input: root = [5,3,6,2,4,null,7], k = 9
Output: true
2 + 7 = 9 (also 3 + 6 and 4 + 5).
Input: root = [5,3,6,2,4,null,7], k = 28
Output: false
The largest possible sum is 6 + 7 = 13.
Constraints
- 1 <= number of nodes <= 10^4
- -10^4 <= Node.val <= 10^4
- All values are unique and form a valid BST.
- -10^5 <= k <= 10^5
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.