#230

Kth Smallest Element in a BST

Medium Inorder with a counter

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the k-th smallest value (1-indexed) in a BST.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the k-th smallest value.

Why this works: inorder visits values in increasing order; stop at the k-th visit.

Example: 3 (1 (-, 2), 4), k = 1

inorder: 1 → first → answer 1

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Collecting all values into a list first — O(n) even when k is small.

Solve on LeetCode ↗
Complexity
  • Time: O(h + k).
  • Space: O(h).
Topic: Binary Search Trees
🎬 Episodes 61–63: video coming soon