Problem
Section titled “Problem”Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
Example 1:

Input: root = [3,1,4,null,2], k = 1Output: 1Example 2:

Input: root = [5,3,6,2,4,null,null,1], k = 3Output: 3Constraints:
- The number of nodes in the tree is
n. -
Node.val
Follow-up: If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”An in-order traversal of a BST (left, node, right) visits the keys in increasing order, so the k-th node it visits holds the k-th smallest value.
We do not need the whole sorted list — only the k-th visit. An iterative in-order traversal with an explicit stack can stop the moment it gets there. Before the first visit it walks down the left spine (up to h nodes); after that each step produces one more visit, so the work is rather than . The explicit stack also avoids deep recursion on a skewed tree.
Example 2 with each node’s in-order position: with k = 3, the traversal pops 1, 2, 3 and returns 3 without touching 4, 5, 6.
Algorithm
Section titled “Algorithm”- Start with an empty stack
standcur = root. - Loop:
- push
curand move tocur->leftuntilcurisnullptr(walk to the smallest unvisited value); - pop the top node
cur, the next value in increasing order. Decrementk; if it reaches0, returncur->val; - move to
cur->right, whose values come next.
- push
Follow-up: frequent updates and queries
Section titled “Follow-up: frequent updates and queries”If the tree changes often and we query often, store in every node the size of its subtree, size(node) = 1 + size(left) + size(right), and keep it up to date during insert and delete (only the nodes on the modified path change). Then the k-th smallest is found by a single descent:
- let
L = size(node->left); - if
k <= L, go left; ifk == L + 1, the answer isnode; otherwise setk -= L + 1and go right.
Each query, insert and delete then costs , which is in a balanced BST (for example a red-black or AVL tree augmented with sizes — an order-statistic tree).
Complexity
Section titled “Complexity”- Time: — at most
hextra nodes are pushed along the first left spine, then each pop is one visit and we stop afterk. - Space: — the stack holds one root-to-node path.
Edge cases
Section titled “Edge cases”k = 1: the first pop is the leftmost node, the minimum (Example 1).k = n: the traversal runs to the rightmost node, the maximum.- Skewed tree: the stack grows to
nentries but there is no recursion, so no risk of stack overflow.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int kthSmallest(TreeNode* root, int k) { stack<TreeNode*> st; TreeNode* cur = root; while (true) { while (cur) { st.push(cur); cur = cur->left; } cur = st.top(); st.pop(); if (--k == 0) return cur->val; cur = cur->right; } }};class Solution { public int kthSmallest(TreeNode root, int k) { ArrayDeque<TreeNode> st = new ArrayDeque<>(); TreeNode cur = root; while (true) { while (cur != null) { st.push(cur); cur = cur.left; } cur = st.pop(); if (--k == 0) return cur.val; cur = cur.right; } }}class Solution: def kthSmallest(self, root: TreeNode | None, k: int) -> int: st = [] cur = root while True: while cur: st.append(cur) cur = cur.left cur = st.pop() k -= 1 if k == 0: return cur.val cur = cur.rightGive it a try first
Attempt the problem on LeetCode — the code will still be here.
