Data Structures19 sections · 729 units
Open in Course

K-th Smallest: Why It Works

Binary search on values

At each node, you're partitioning values into lower half (left) and upper half (right).

The count of elements going left tells us: are the first kk smallest values all in the left subtree?

  • If yes (kleftCountk \leq \text{leftCount}): recurse left with same kk
  • If no (k>leftCountk > \text{leftCount}): recurse right with kleftCountk - \text{leftCount}

You're doing binary search on the value space, guided by position counts.

Each level eliminates half the values. After logσ\log \sigma levels, you reach a leaf containing the answer.