A binary search tree contains distinct elements. What is the time complexity of picking an element in that is smaller than the maximum element in ?
View worked solution
Answer: D
To find any element in a Binary Search Tree (BST) that is strictly smaller than the maximum element, we can utilize the structural properties of a BST. Assuming (so that such an element always exists):
Let the root of the BST be . We can determine a valid element in time by checking the right child of :
- Case 1: has a right child ()
The maximum element of the BST must reside in the right subtree of . Since all elements in the right subtree are strictly greater than , the root itself is guaranteed to be smaller than the maximum element. Thus, we can simply return . - Case 2: does not have a right child ()
The root itself is the maximum element of the BST. Since , the left child of must exist. Any element in the left subtree is strictly smaller than (the maximum). Thus, we can simply return the left child of .
In both cases, we only need to inspect the root and its immediate children. This requires a constant number of operations, resulting in a time complexity of .
Free practice · Your answers on this page are not saved.
