Kth Smallest Element in a BST

Data Structures
Medium
Meta
94.8K views

Given the root of a BST and an integer $k$, find the $k$-th smallest element. Use in-order traversal (DFS) to leverage the BST property.

Why Interviewers Ask This

Meta interviewers ask this to evaluate your ability to leverage specific data structure properties rather than relying on brute force. They want to see if you recognize that an in-order traversal of a Binary Search Tree yields sorted values, allowing for an O(N) or O(H) solution depending on implementation. This tests your depth of understanding regarding tree mechanics and your skill in optimizing space complexity through recursion or iteration.

How to Answer This Question

1. Clarify constraints: Immediately confirm if the tree is static or dynamic, and whether k is guaranteed to be valid within the node count. Meta values clarity before coding. 2. Explain the core property: State clearly that an in-order traversal (Left-Root-Right) visits nodes in ascending order because of the BST invariant. 3. Propose the recursive strategy: Describe visiting the left subtree first. If the count reaches zero, return the current node's value. Increment a counter after processing the left side. 4. Discuss optimization: Mention how to stop early once the k-th element is found, avoiding full tree traversal, which demonstrates efficiency awareness. 5. Address iterative alternative: Briefly note how to achieve the same result with an explicit stack to avoid recursion depth issues on very deep trees, showing robustness.

Key Points to Cover

  • Explicitly state that in-order traversal guarantees sorted output due to BST properties
  • Demonstrate early termination logic to optimize performance once k is found
  • Clarify time complexity as O(H) for balanced trees versus O(N) worst case
  • Discuss handling edge cases like invalid k values or empty trees upfront
  • Show awareness of recursion stack depth implications on large datasets

Sample Answer

To solve this efficiently, I would leverage the fundamental property of a Binary Search Tree where an in-order traversal produces elements in strictly non-decreasing order. My approach involves a recursive depth-first search. First, I define a helper function that takes the current node and a reference to a counter. I start by recursively traversing the left subtree. Once the left recursion returns, I increment my counter. If the counter equals k at this moment, I have found our target, so I store the current node's value and immediately return to unwind the stack without visiting the right subtree. If the counter hasn't reached k yet, I proceed to traverse the right subtree. For example, if k is 3 and the tree has nodes [3, 1, 4, null, null, 2], the traversal visits 1, then 2, then 3. The third visit matches k, so we return 3. This method ensures we only visit necessary nodes, achieving O(H) time complexity in balanced trees and O(1) auxiliary space if we ignore the call stack, though O(H) space is required for the recursion. This approach aligns well with Meta's focus on algorithmic efficiency and clean code structure.

Common Mistakes to Avoid

  • Converting the entire tree to an array first, which wastes O(N) space instead of stopping early
  • Ignoring the BST property and using a generic sorting algorithm on collected values
  • Failing to handle the case where k exceeds the total number of nodes in the tree
  • Not clarifying whether the interviewer prefers a recursive or iterative stack-based solution

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 71 Meta questions