AlgoFrames

Cost of search in a chain

Warm-up · Multiple choice · about 2 min

A BST was built by inserting the keys 1, 2, …, n in increasing order. What is the worst-case cost of searching it for a key?

Example 1

Input:

[[1,2,3,4,5]]

Output:

O(n)

With n = 5, finding 5 passes all 5 nodes.

Constraints

  • No rebalancing happens

Learn the idea first: Why balance matters, a visual lesson.

More Why balance matters problems