Problem Description
Given the root of a binary search tree (BST) and an integer k, return the kth smallest value in the BST (1-indexed). Since an in-order traversal of a BST naturally retrieves the nodes in sorted order, we can leverage this property to efficiently solve the problem.
Key Insights
- An in-order traversal of a BST visits nodes in increasing order.
- Use a counter to keep track of the number of nodes visited.
- As soon as the counter reaches k, the current node’s value is the kth smallest.
- For frequent modifications (insertions/deletions) and queries, consider augmenting the BST with subtree counts.
Space and Time Complexity
Time Complexity: O(n) in the worst-case scenario (when k ≈ n), but on average the traversal may stop early. Space Complexity: O(h) where h is the height of the tree (O(n) in the worst-case for a skewed tree).
Solution
We perform an in-order traversal of the BST which visits nodes in ascending order. During the traversal, we maintain a counter (or decrement k) to check when we have encountered the kth smallest element. When the counter reaches 0 (or k becomes 0), we capture the current node’s value as our answer and return it. This approach is both simple and efficient for the given constraints.