Dsa
Binary Search Tree
Easy
Corporate Subtree Budget
A company uses IDs to represent employee seniority in a BST. Each ID is also its budget. Given a target value X, first find the 'Anchor Node', which is the node with the smallest ID such that ID >= X. If such a node exists, calculate the sum of all IDs in the subtree rooted at that Anchor Node. If no such node exists, output 0.
Example:
Input: 5 2 8 1 3; 2
Output: 6
Explanation: The smallest ID >= 2 is 2. The subtree at 2 includes 2, 1, and 3. Sum: 2+1+3 = 6.
Key concepts
binary_search_treerecursionsuccessor
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now