StreakPeaked· Practice

ExamsGATETechnical

A binary search tree T contains n distinct elements. What is the time complexity of picking an element in T that is smaller than the maximum element in T?

  1. Θ(n log n)
  2. Θ(n)
  3. Θ(log n)
  4. Θ(1)

Correct answer: Θ(log n)

Solution

To pick an element smaller than the maximum, one can traverse to the rightmost node and then choose its parent or another suitable node. In a balanced BST, this takes proportional to the height of the tree, which is Θ(log n).

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →