Introduction
LeetCode 230 – Kth Smallest Element in a BST is one of the most important Binary Search Tree interview problems.
This question is popular because it tests:
- BST properties
- Inorder traversal
- DFS recursion
- Tree traversal optimization
- Recursive state management
Understanding this problem properly builds a strong foundation for advanced BST problems.
Problem Link
🔗 https://leetcode.com/problems/kth-smallest-element-in-a-bst/
Problem Statement
Given:
- Root of a Binary Search Tree
- Integer
k
Return:
The indexing is:
Example 1
Input
Output
Explanation
BST inorder traversal becomes:
1st smallest element is:
Example 2
Input
Output
Key Observation
The most important BST property:
Example:
Inorder traversal:
So:
Intuition
We perform:
While traversing:
- Keep counting visited nodes
- When count becomes
k - Store answer
No need to traverse entire tree after finding answer.
Brute Force Approach
Idea
- Store complete inorder traversal in list
- Return:
Brute Force Java Solution
Complexity of Brute Force
Time Complexity
Space Complexity
Extra list storage required.
Optimized Recursive Approach
Idea
Instead of storing entire traversal:
- Maintain counter
- Stop when kth node is reached
This saves unnecessary storage.
Java Solution
Cleaner Optimized Version
Why This Works
BST inorder traversal always visits nodes in:
So:
- 1st visited node = smallest
- 2nd visited node = second smallest
- kth visited node = kth smallest
Dry Run
Input
BST Structure
Inorder Traversal
Counter Progress
| Node | Count |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
At count = 3:
Final Output
Iterative Stack Approach
Idea
Use explicit stack instead of recursion.
Iterative Java Solution
Time Complexity Analysis
Optimized Recursive
Time Complexity
Where:
H= tree height- We visit only required nodes
Worst case:
Space Complexity
Recursive stack space.
Iterative Complexity
Time Complexity
Space Complexity
Stack space.
Follow-Up Optimization
Problem Follow-Up
What if BST changes frequently?
Example:
- Insert operations
- Delete operations
- Frequent kth smallest queries
Advanced Optimization
Store:
inside every node.
This allows:
kth smallest queries.
This concept is used in:
- Order Statistic Trees
- Augmented BSTs
- Indexed Trees
Interview Explanation
In interviews, explain:
Inorder traversal of a BST gives nodes in sorted order. Therefore, the kth visited node during inorder traversal is the kth smallest element.
This demonstrates:
- BST understanding
- DFS recursion
- Tree traversal mastery
- Optimization thinking
Common Mistakes
1. Forgetting BST Property
This solution works because BST inorder traversal is sorted.
Not true for normal binary trees.
2. Using Extra Array Unnecessarily
Optimized approach avoids storing entire traversal.
3. Incorrect Counter Placement
Counter must increase:
4. Forgetting Early Return
Once kth element is found:
FAQs
Q1. Why does inorder traversal work?
Because BST inorder traversal produces sorted order.
Q2. Can this be solved iteratively?
Yes.
Using stack-based inorder traversal.
Q3. Why is BST important here?
Without BST ordering property:
Q4. Is this frequently asked?
Yes.
It is one of the most common BST interview questions.
Conclusion
LeetCode 230 is an excellent BST problem for mastering:
- Inorder traversal
- BST properties
- DFS recursion
- Stack traversal
- Tree optimization
The core insight is:
Inorder traversal of a BST always produces sorted order.
Once this concept becomes intuitive, many BST interview problems become much easier.





