LeetCode Problem

How to Solve Kth Smallest Element in a BST

To solve the Kth Smallest Element in a BST problem, perform an in-order traversal while counting nodes visited. Use the BST property to avoid traversing unnecessary branches and stop early once the kth element is reached. This ensures an efficient solution without needing to store all values in a separate list, balancing time and space complexity for large trees.

GhostInterview Help

Need help with Kth Smallest Element in a BST without spending extra time grinding it?

GhostInterview can read Kth Smallest Element in a BST from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #230Binary-tree traversal and state trackingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Binary-tree traversal and state tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the Kth Smallest Element in a BST problem, perform an in-order traversal while counting nodes visited. Use the BST property to avoid traversing unnecessary branches and stop early once the kth element is reached. This ensures an efficient solution without needing to store all values in a separate list, balancing time and space complexity for large trees.

Problem Statement

Given the root of a binary search tree and an integer k, return the kth smallest value among all node values. The tree follows standard BST properties where left children are smaller than the node and right children are larger.

You must implement an approach that efficiently finds this element without fully flattening the tree, making use of in-order traversal and careful state tracking to minimize unnecessary recursion and storage.

Examples

Example 1

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

Output: 1

Example details omitted.

Example 2

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

Output: 3

Example details omitted.

Constraints

  • The number of nodes in the tree is n.
  • 1 <= k <= n <= 104
  • 0 <= Node.val <= 104

Solution Approach

Recursive In-Order Traversal with Counter

Perform a standard in-order traversal, incrementing a counter at each node. Once the counter equals k, record the node's value and terminate recursion to avoid extra work.

Iterative In-Order Traversal using Stack

Use an explicit stack to simulate in-order traversal iteratively. Push left children, process nodes while counting, and stop when the kth element is reached, reducing call stack overhead.

Optimized BST with Node Counts

Augment the BST nodes with subtree sizes to allow O(log n) navigation. Compare k with left subtree counts to move left or right, efficiently jumping over unneeded nodes.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity ranges from O(h + k) for recursive or iterative traversal, where h is tree height, to O(log n) per query with augmented BST. Space complexity is O(h) for recursion stack or stack in iteration, or O(1) additional if using counts in augmented nodes.

What Interviewers Usually Probe

  • Are you leveraging the BST property to avoid unnecessary traversal?
  • How do you track the count without storing all elements?
  • Can you optimize for repeated kth queries with minimal extra storage?

Common Pitfalls or Variants

Common pitfalls

  • Confusing in-order traversal order in a BST and miscounting nodes.
  • Using full tree flattening which increases space unnecessarily.
  • Failing to stop traversal after reaching the kth element, wasting time.

Follow-up variants

  • Find kth largest element in a BST by reversing in-order traversal.
  • Handle dynamic BSTs with insertions and deletions while supporting kth smallest queries.
  • Compute kth smallest element in a BST with duplicates allowed, considering duplicate counts.

How GhostInterview Helps

  • Provides step-by-step traversal tracking with early stopping conditions for the kth node.
  • Highlights failure modes like miscounting or over-traversing subtrees to avoid wasted effort.
  • Suggests optimal iterative and augmented BST strategies to improve efficiency and reduce stack usage.

Topic Pages

FAQ

What is the main strategy for finding the kth smallest element in a BST?

Use in-order traversal while maintaining a counter; the kth visited node is the answer.

Can this problem be solved iteratively without recursion?

Yes, a stack can simulate in-order traversal, counting nodes until reaching k.

How does BST structure help in this problem?

BST property allows skipping entire subtrees that do not contain the kth element, reducing traversal.

What is the time complexity for this approach?

O(h + k), where h is tree height, because traversal stops once the kth element is found.

Are there variants of this problem using similar traversal patterns?

Yes, such as kth largest element, BSTs with duplicates, or dynamic BST queries.

GhostInterview Solver

Need direct help with Kth Smallest Element in a BST instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Kth Smallest Element in a BST from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.