Kth Smallest Element in a BST — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “Find the kth smallest element in a BST.” The answer comes straight from one property of BSTs: in-order traversal visits nodes in ascending sorted order. The kth node visited in-order is the kth smallest. This is really a test of whether you know your BST traversal cold.
The Problem
Given the root of a binary search tree and an integer k, return the kth smallest value (1-indexed) among all node values.
Example
Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| BST in-order property | Do you immediately connect “kth smallest” to in-order traversal? |
| Early termination | Do you stop after k nodes, not traverse the whole tree? |
| Iterative vs. recursive | Can you implement both? Iterative is preferred for early exit. |
| Follow-up: frequent reads | Do you know the augmented-tree approach for O(log n) queries? |
Step 1: Clarifying Questions
- Is k always valid? Yes —
1 <= k <= number of nodes. - Are node values unique? Yes — BST nodes have distinct values.
Step 2: The Key Insight
In-order traversal of a BST visits nodes in ascending sorted order. The kth node visited is the kth smallest. No sorting required — the BST structure gives you sorted order for free.
---
Step 3: Algorithm
Use an explicit stack for iterative in-order traversal. Push left nodes until null, then pop and visit, decrement k, push right child. Stop when k == 0.
Python Implementation
Iterative (preferred — cleanest early exit):
def kthSmallest(root, k: int) -> int:
stack = []
current = root
while current or stack:
while current:
stack.append(current)
current = current.left
current = stack.pop()
k -= 1
if k == 0:
return current.val
current = current.right
Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(H + k) — H is tree height, stop after k nodes |
| Space | O(H) — stack depth |
Common Interview Mistakes
-
Traversing the whole tree. Stop as soon as k hits 0. No need to visit remaining nodes.
-
Collecting all values then indexing.
values[k-1]works but is O(n) time and space. The iterative approach terminates early. -
Not knowing the follow-up. If the BST is modified frequently and
kthSmallestis called many times, augment each node withleft_count. Query becomes O(log n).
What a Strong Candidate Sounds Like
“In-order traversal of a BST visits nodes in ascending order, so I want the kth node visited. I’ll use an iterative approach with an explicit stack — push lefts, pop and count, move right. I stop the moment k hits zero. Time is O(H + k), space O(H).”
Example Interview Dialogue
Interviewer: What if this is called millions of times on a frequently updated BST?
Candidate: I’d augment each node with the count of nodes in its left subtree. On insert or delete, update counts along the path — O(log n). On query, binary-search: if k == left_count + 1 the current node is the answer; if k <= left_count go left; else go right with k = k - left_count - 1. Each query becomes O(log n).
Related Problems to Practice
- Validate Binary Search Tree — In-order produces sorted sequence; use to validate.
- Lowest Common Ancestor of a BST — BST path properties for LCA.
- Invert Binary Tree — Tree traversal fundamentals.
Practice This in a Mock Interview
Kth Smallest in a BST is short to implement but interviewers always push the follow-up. Have the augmented-tree O(log n) answer ready.
Start a mock interview for Kth Smallest Element in a BST on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub