230. Kth Smallest Element in a BST
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Trees
- Time
- —
- Space
- —
Standard Solution (C++)
SummaryGiven a binary search tree, find the value of the k-th smallest node by rank (1-indexed). Since a BST's in-order traversal yields values in ascending order, the k-th visited node is exactly the answer.
Approach
- Perform an iterative in-order traversal (left → root → right) using an explicit stack, which visits nodes in ascending value order.
- Decrement a counter each time a node is popped and processed; when the counter reaches zero, the current node holds the k-th smallest value.
- The iterative approach avoids recursion overhead and makes early termination clean — we simply return the moment
khits zero without continuing the traversal. - Time is O(H + k) because we descend to the leftmost node in O(H) steps, then visit exactly k nodes.
#include <stack>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> stk;
TreeNode* cur = root;
// Iterative in-order traversal: visits nodes in ascending BST order.
while (cur || !stk.empty()) {
// Descend as far left as possible.
while (cur) {
stk.push(cur);
cur = cur->left;
}
// Process the next smallest node.
cur = stk.top();
stk.pop();
if (--k == 0) return cur->val; // Found the k-th smallest.
// Move to the right subtree.
cur = cur->right;
}
return -1; // Unreachable if k is valid.
}
};
Time O(H + k) Space O(H)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void solve(TreeNode* root, int& k, int& res) {
if(!root){
return;
}
solve(root->left, k, res);
if(k==0){
return;
}
if(--k==0){
res=root->val;
return;
}
solve(root->right, k, res);
}
int kthSmallest(TreeNode* root, int k) {
int res=-1;
solve(root, k, res);
return res;
}
};
// Time: O(H+k), where H is the height of the tree, since we may need to traverse down to the leftmost node and then visit k nodes.
// Space: O(H), where H is the height of the tree, due to the recursionn stack. In the worst case of a skewed tree, this could be O(N). My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.