Dsa
Binary Search Tree
Medium
LCA Subtree Aggregate
In a corporate hierarchy represented as a BST of employee IDs, find the 'Scope of Influence' for two specific employees. The Scope of Influence is defined as the sum of all employee IDs in the subtree rooted at the Lowest Common Ancestor (LCA) of the two given IDs.
Input: The first line contains space-separated integers to build the BST (inserted in order). The second line contains two integers U and V (the target IDs).
Output: A single integer representing the sum of IDs in the LCA's subtree.
Example:
Input:
10 5 15 2 7 12 20
2 7
Output: 14
Explanation: LCA of 2 and 7 is 5. The subtree rooted at 5 contains {5, 2, 7}. Sum = 14.
Key concepts
binary_search_treelowest_common_ancestorrecursion
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