Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Trees / DFS High frequency Solution Available Practice Mode

Kth Smallest Element in a BST LeetCode Solution

Tags: #Tree #DFS #BST #Binary Tree

Approach Summary

In-order traversal of BST visits nodes in ascending order. Count k nodes; return on the kth.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Trees / DFS Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Trees / DFS pattern.

Framework: Trees / DFS • Interview Prep: Tested at FAANG • Explore all Trees / DFS problems & invariants →

How to Recognize This Pattern

  • "kth element in BST"
  • In-order traversal of BST = sorted ascending

Complexity Analysis

Time Complexity

O(H + k)

Space Complexity

O(H)

Tags

Tree DFS BST Binary Tree

Frequently Asked Questions

What is the optimal time and space complexity for Kth Smallest Element in a BST?
The optimal solution for Kth Smallest Element in a BST achieves O(H + k) runtime complexity and requires O(H) auxiliary space. Time complexity is O(H + k) where H is tree height, because we only visit the first k nodes in sorted order. Space complexity is O(H) for the call stack.
How do I identify that Kth Smallest Element in a BST requires the Trees / DFS pattern?
In technical interviews, look for key problem signals: "kth element in BST"; In-order traversal of BST = sorted ascending. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Trees / DFS strategy.
What critical edge cases must be handled in Kth Smallest Element in a BST?
Essential edge cases to verify include: k = 1 (smallest element; leftmost node in tree); k equals total number of nodes (largest element; rightmost node); Left-skewed tree. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Kth Smallest Element in a BST?
Kth Smallest Element in a BST is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Facebook, Microsoft.
Buy me a coffee
Daily Practice
0/0