Skip to content
AI360Xpert
Beta
Difficulty: MediumTrees

Kth Smallest Element in a BST

Problem in Plain English

Given the root of a binary search tree and an integer k, return the kth smallest value using the fact that an inorder walk visits values in ascending order. Every k between 1 and the tree size is valid.

Problem Statement

Given the root of a binary search tree and an integer k, return the kth smallest value in the tree, counting from 1.

A binary search tree keeps smaller values on the left and larger values on the right, so an inorder walk of left, node, then right visits values in strictly increasing order.

Constraints

  • 1 <= k <= n <= 10^4, where n is the number of nodes
  • 0 <= Node.val <= 10^4
  • The tree is a valid binary search tree

Examples

Example 1

Input
root = [5,3,6,2,4,null,null,1], k = 3
Output
3

Inorder gives [1,2,3,4,5,6]. Counting from one, the visits are 1, then 2, then 3, so rank three returns 3. The root value is not its rank.

Example 2

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

Inorder gives [1,2,3,4], so rank one returns 1. Its right child 2 comes after 1; early stopping returns before entering that right subtree.

Example 3

Input
root = [0], k = 1
Output
0

A single node is both the smallest and largest value. Rank one returns 0; zero is a valid answer, not a missing-node marker.

Example 4

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

This right-only chain has inorder [1,2,3,4]. Since k equals the node count, early stopping must visit every node before returning 4.

Intuition

A rank is a position in sorted order, not a distance from the root. In a binary search tree, every left-subtree value is smaller than its root and every right-subtree value is larger. Visiting left subtree, node, then right subtree therefore produces sorted values without sorting. In Example 1, this order is [1,2,3,4,5,6]; the third visit is 3, although the root is 5. A stack remembers ancestors waiting for their left subtrees to finish. We can either collect all values or count visits and stop at the requested rank.

Approaches

Full Inorder Collect

Solution Details

Reveal Full Inorder Collect: intuition, complexity, and code

Hints

Hint 1
Which traversal of a BST visits values in sorted order? A breadth-first walk does not preserve ranks.
Hint 2
Think of k as the number of inorder visits needed, rather than a target value or root depth.
Hint 3
Keep ancestors on a stack while moving left; pop one visit, then move right. Count only pops and return immediately on visit k.

Edge Cases

  • A single node with value 0 and k = 1 returns 0; test nodes for null rather than testing their values.
  • For k = 1, return the leftmost value. A 10,000-node left chain still requires 10,000 pushes before the first pop.
  • For k = n, the answer is the rightmost value and every node is visited, so early stopping saves no visits.
  • Left-only and right-only chains are valid BSTs. Explicit stacks handle their maximum height without recursive call-stack limits.
  • Rank can fall on an internal node with a right child, as 3 does in Example 1. Return before traversing that larger subtree.

Common Mistakes and Interview Tips

  • Using order[k] instead of order[k - 1] confuses one-based ranks with zero-based indices.
  • Incrementing the counter on push counts deferred ancestors before smaller descendants; Example 1 would count root 5 first.
  • Moving right before visiting the popped node changes inorder to left, right, node and destroys sorted order.
  • Claiming O(k) time ignores the initial left descent: k = 1 on a left chain takes O(n). Claiming O(log n) stack space assumes balance, which is not guaranteed.
  • Using recursive collection without discussing runtime limits can fail on the allowed 10,000-node chain. The published collectors use explicit stacks.
  • Keeping count or collected values on the Solution object without resetting them corrupts repeated calls. These snippets use invocation-local state.
  • For repeated updates and rank queries, augment each node with its subtree size. Compare k with left size + 1 to choose a branch; subtract left size + 1 when moving right. Maintain sizes through insertions, deletions and rotations. Queries take O(h), becoming O(log n) only with balance.

Key Takeaway

A BST turns inorder traversal into a sorted stream. An explicit stack preserves the next visit without copying or modifying the tree, and a rank query can stop after k visits. This shortcut relies on the BST property; arbitrary binary trees do not have sorted inorder values.