Kth Smallest Element in a BST
Imagine you have a sorted collection of numbers stored efficiently in a binary search tree, and you want to quickly find the kth smallest number without flattening the entire tree.
Given the root of a binary search tree (BST) and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
The number of nodes in the tree is n, where 1 ≤ n ≤ 10^51 ≤ k ≤ nNode values are unique integersdef kthSmallest(root: Optional[TreeNode], k: int) -> int:public int kthSmallest(TreeNode root, int k)int kthSmallest(TreeNode* root, int k)function kthSmallest(root, k)def kthSmallest(root: Optional[TreeNode], k: int) -> int:
# Write your solution here
passclass Solution {
public int kthSmallest(TreeNode root, int k) {
// Write your solution here
return 0;
}
}#include <vector>
using namespace std;
int kthSmallest(TreeNode* root, int k) {
// Write your solution here
return 0;
}function kthSmallest(root, k) {
// Write your solution here
}0Returning default or uninitialized value when traversal is incomplete or k is out of range.✅ Ensure traversal visits nodes and returns elements[k-1] after full or partial traversal.last node valueTraversal does not stop early and returns last visited node instead of k-th.✅ Add early stopping condition when count == k during traversal.off-by-one element (e.g., k+1 or k-1 element)Incorrect indexing due to confusion between 0-based and 1-based indexing.✅ Use zero-based indexing with k-1 or count nodes until count == k.wrong node value in skewed treeTraversal misses nodes in skewed trees due to incorrect recursion or iteration.✅ Ensure traversal visits all nodes including null left or right children properly.TLEFull traversal of large tree without early stopping causes timeout.✅ Implement early stopping in traversal to achieve O(H + k) time complexity.{"root":[3,1,4,null,2],"k":1}1The in-order traversal of the BST is [1,2,3,4]. The 1st smallest element is 1.
{"root":[5,3,6,2,4,null,null,1],"k":3}3In-order traversal is [1,2,3,4,5,6]. The 3rd smallest element is 3.
{"root":[1],"k":1}1Single node tree; the only node is the 1st smallest.
{"root":[1,null,2,null,3,null,4,null,5],"k":5}5Right skewed tree with nodes 1 to 5; 5th smallest is 5.
{"root":[5,4,null,3,null,2,null,1],"k":1}1Left skewed tree with nodes 1 to 5; smallest is 1.
{"root":[3,1,4,null,2],"k":2}2In-order traversal is [1,2,3,4]. The 2nd smallest element is 2.
{"root":[2,1,3],"k":3}3In-order traversal is [1,2,3]. The 3rd smallest element is 3.
{"root":[5,3,6,2,4,null,null,1],"k":4}4In-order traversal is [1,2,3,4,5,6]. The 4th smallest element is 4.
{"root":[50000,25000,75000,12500,37500,62500,87500,6250,18750,31250,43750,56250,68750,81250,93750,3125,9375,15625,21875,28125,34375,40625,46875,53125,59375,65625,71875,78125,84375,90625,96875,1562,4687,7812,10937,14062,17187,20312,23437,26562,29687,32812,35937,39062,42187,45312,48437,51562,54687,57812,60937,64062,67187,70312,73437,76562,79687,82812,85937,89062,92187,95312,98437,781,2343,3906,5468,7031,8593,10156,11718,13281,14843,16406,17968,19531,21093,22656,24218,25781,27343,28906,30468,32031,33593,35156,36718,38281,39843,41406,42968,44531,46093,47656,49218,50781,52343,53906,55468,57031,58593,60156,61718,63281,64843,66406,67968,69531,71093,72656,74218,75781,77343,78906,80468,82031,83593,85156,86718,88281,89843,91406,92968,94531,96093,97656,99218],"k":100}nullLarge balanced BST with n=100 nodes; solution must run in O(H + k) or O(n) time within 2 seconds.
