PROBLEM 29 OF 75

Kth Smallest Element in a BST

Return the kth value in BST sorted order.

PATTERNTrees & recursive traversalDefine the meaning of one recursive return value.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Return the kth value in BST sorted order.

WHY THIS FITS

In-order traversal visits BST values in ascending order.

COMPLEXITY

O(h + k) time · O(h) space

COMMON FAILURE

Stopping after k pushes confuses discovery with visitation.

THE REASONING, IN ONE PLACE

How to solve Kth Smallest Element in a BST

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Return the kth value in BST sorted order.

Why Trees & recursive traversal fits

In-order traversal visits BST values in ascending order.

State to maintain

An explicit stack, current node, and remaining rank.

Transition

Push left paths, pop one value at a time, decrement k, then explore right.

Time and space

O(h + k) time · O(h) space

Counterexample to the tempting mistake

Stopping after k pushes confuses discovery with visitation.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗