Find kth smallest element in a binary search tree in Optimum way
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the kth smallest element in a Binary Search Tree (BST) is a common algorithmic problem. A Binary Search Tree is a binary tree with the property that the left child of any node contains only nodes with values less than the parent node and the right child contains only nodes with values greater than the parent node. The task of finding the kth smallest element can be optimized by taking advantage of this structure. Below, we will explore this problem in detail, providing examples, technical explanations, and summative tables to enhance understanding.
Understanding the Binary Search Tree
A Binary Search Tree consists of nodes with each node having:
- A value
- A left child node
- A right child node
The in-order traversal of a BST (left, root, right) accesses node values in ascending order, which is exactly the order needed to find the kth smallest element.
Optimized Approach to Find the kth Smallest Element
1. In-order Traversal
The simplest approach is to perform an in-order traversal of the tree, which by definition visits the nodes in ascending order.
- Time Complexity: , where is the number of nodes in the tree.
- Space Complexity: due to the recursion stack or the list storing the nodes' values.
Example
2. Enhanced In-order Traversal with Early Stopping
To improve efficiency and avoid unnecessary operations, we can modify the in-order traversal to stop as soon as the kth element is reached.
Implementation
- Time Complexity: , where is the height of the tree. This is because we might have to traverse up to
knodes, and the depth of the recursion can go at most to the tree's height. - Space Complexity: due to the recursion stack, where is the height of the tree.
Considerations
- Unbalanced Trees: For unbalanced trees, the worst-case time complexity may still be encountered due to tree height.
- Balanced Trees: Selbst-balancing trees (like AVL or Red-Black Trees) ensure height, thus optimizing search time to .
Advanced Topic: Maintaining Node Counts
An advanced method involves augmenting each node with a subtree size attribute, allowing constant-time determination of the number of nodes in a subtree.
Augmented Node Structure
Procedure
- For each node, maintain the count of nodes in its left subtree.
- Use the counts to direct the in-order sequence without traversing all nodes.
Implementation
- Time Complexity: Average case for balanced trees.
- Space Complexity: additional space beyond the tree nodes.
Summary Table
| Method | Time Complexity | Space Complexity | Notes |
| Full In-order Traversal | (recursion) | Traverses the whole tree; simplified implementation. | |
| Enhanced In-order | (recursion) | Stops traversal early; optimal for small k. | |
| Augmented Nodes with Count | additional space | Optimal for trees with node counts precomputed. Requires additional node management. |
Conclusion
The Binary Search Tree structure inherently supports efficient kth smallest element queries by leveraging in-order properties. The choice of method relies heavily on the nature of the input tree, whether it is balanced and the value of k relative to N. For dynamic or frequently queried trees, maintaining subtree sizes can offer substantial performance gains.
Related reading
- find kth smallest number in Ologn time
- Find largest rectangle containing only zeros in an N×N binary matrix
- Find length of smallest window that contains all the characters of a string in another string
- Find local minima in an array
- find lowest index of a given value in a presorted array
- Find maximum value in an array by recursion
- Find local minimum in n x n matrix in On time
- Find longest increasing sequence

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.