View worked solution
Answer: 110
To find the sum of all elements in a balanced binary search tree (BST) of size that fall within the range (where there are such elements), we can perform a standard range search:
- Search for the boundaries: We search for and in the BST. Since the tree is balanced, the height of the tree is . Finding the split node and traversing down to the boundaries takes time.
- Traverse and sum the elements: The elements lying strictly between the search paths of and belong to subtrees that are completely inside the range . We can traverse these subtrees to sum their elements. Since there are exactly such elements, traversing them takes time.
Thus, the tightest upper bound on the time complexity to compute the sum is:
We are given the upper bound in the form:
Comparing with :
- For the first term , we get:
and - For the second term , we get:
and
Now, we calculate the required value:
Free practice · Your answers on this page are not saved.
