k-th Smallest in BST (Order Statistics in BST)
Last Updated : 03 Feb, 2025
Given a Binary Search Tree (BST) and a positive integer k, find the kth smallest element in the Binary Search Tree.
Example:
Input: k = 3

Output: 10
Explanation: The inorder traversal of given BST is [4, 8, 10, 12, 14, 20, 22] and its 3rd smallest element is 10.
[Expected Approach] Using In-Order traversal - O(k) Time and O(h) Space
The idea is to traverse the binary search tree using in-order traversal while maintaining the count of nodes traversed. If the node count becomes equal to k, then return the node.
C++ //Driver Code Starts // C++ program to find kth // smallest value in BST #include <iostream> using namespace std; class Node { public: int data; Node* left; Node* right; Node(int x) { data = x; left = nullptr; right = nullptr; } }; //Driver Code Ends // Recursive function for inorder traversal of the tree and // find its kth smallest value. // Returns -1 if value is not found. int kthSmallestRecur(Node* root, int &cnt, int k) { if (root == nullptr) return -1; // Process left subtree. int left = kthSmallestRecur(root->left, cnt, k); // If kth smallest is found in left // subtree, then return it. if (left != -1) return left; // increment node count cnt++; // If curr node is kth smallest, // return it. if (cnt == k) return root->data; // Else process the right subtree // and return its value. int right = kthSmallestRecur(root->right, cnt, k); return right; } // Function to find kth smallest value in BST. int kthSmallest(Node* root, int k) { int cnt = 0; return kthSmallestRecur(root, cnt, k); } //Driver Code Starts int main() { // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 Node* root = new Node(20); root->left = new Node(8); root->right = new Node(22); root->left->left = new Node(4); root->left->right = new Node(12); root->left->right->left = new Node(10); root->left->right->right = new Node(14); int k = 3; cout << kthSmallest(root, k) << endl; return 0; } //Driver Code Ends
Java //Driver Code Starts // Java program to find kth // smallest value in BST class Node { int data; Node left, right; Node(int x) { data = x; left = null; right = null; } } class GfG { //Driver Code Ends // Recursive function for inorder traversal of the tree // and find its kth smallest value. // Returns -1 if value is not found. static int kthSmallestRecur(Node root, int[] cnt, int k) { if (root == null) return -1; // Process left subtree. int left = kthSmallestRecur(root.left, cnt, k); // If kth smallest is found in left // subtree, then return it. if (left != -1) return left; // increment node count cnt[0]++; // If curr node is kth smallest, // return it. if (cnt[0] == k) return root.data; // Else process the right subtree // and return its value. int right = kthSmallestRecur(root.right, cnt, k); return right; } // Function to find kth smallest value in BST. static int kthSmallest(Node root, int k) { int[] cnt = {0}; return kthSmallestRecur(root, cnt, k); } //Driver Code Starts public static void main(String[] args) { // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 Node root = new Node(20); root.left = new Node(8); root.right = new Node(22); root.left.left = new Node(4); root.left.right = new Node(12); root.left.right.left = new Node(10); root.left.right.right = new Node(14); int k = 3; System.out.println(kthSmallest(root, k)); } } //Driver Code Ends
Python #Driver Code Starts # Python program to find kth # smallest value in BST class Node: def __init__(self, x): self.data = x self.left = None self.right = None #Driver Code Ends # Recursive function for inorder traversal of the tree # and find its kth smallest value. # Returns -1 if value is not found. def kthSmallestRecur(root, cnt, k): if root is None: return -1 # Process left subtree. left = kthSmallestRecur(root.left, cnt, k) # If kth smallest is found in left # subtree, then return it. if left != -1: return left # increment node count cnt[0] += 1 # If curr node is kth smallest, # return it. if cnt[0] == k: return root.data # Else process the right subtree # and return its value. right = kthSmallestRecur(root.right, cnt, k) return right # Function to find kth smallest value in BST. def kthSmallest(root, k): cnt = [0] return kthSmallestRecur(root, cnt, k) #Driver Code Starts if __name__ == "__main__": # Binary search tree # 20 # / \ # 8 22 # / \ # 4 12 # / \ # 10 14 root = Node(20) root.left = Node(8) root.right = Node(22) root.left.left = Node(4) root.left.right = Node(12) root.left.right.left = Node(10) root.left.right.right = Node(14) k = 3 print(kthSmallest(root, k)) #Driver Code Ends
C# //Driver Code Starts // C# program to find kth // smallest value in BST using System; class Node { public int data; public Node left, right; public Node(int x) { data = x; left = null; right = null; } } class GfG { //Driver Code Ends // Recursive function for inorder traversal of the tree // and find its kth smallest value. // Returns -1 if value is not found. static int kthSmallestRecur(Node root, ref int cnt, int k) { if (root == null) return -1; // Process left subtree. int left = kthSmallestRecur(root.left, ref cnt, k); // If kth smallest is found in left // subtree, then return it. if (left != -1) return left; // increment node count cnt++; // If curr node is kth smallest, // return it. if (cnt == k) return root.data; // Else process the right subtree // and return its value. int right = kthSmallestRecur(root.right, ref cnt, k); return right; } // Function to find kth smallest value in BST. static int kthSmallest(Node root, int k) { int cnt = 0; return kthSmallestRecur(root, ref cnt, k); } //Driver Code Starts static void Main(string[] args) { // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 Node root = new Node(20); root.left = new Node(8); root.right = new Node(22); root.left.left = new Node(4); root.left.right = new Node(12); root.left.right.left = new Node(10); root.left.right.right = new Node(14); int k = 3; Console.WriteLine(kthSmallest(root, k)); } } //Driver Code Ends
JavaScript //Driver Code Starts // JavaScript program to find kth // smallest value in BST class Node { constructor(x) { this.data = x; this.left = null; this.right = null; } } //Driver Code Ends // Recursive function for inorder traversal of the tree // and find its kth smallest value. // Returns -1 if value is not found. function kthSmallestRecur(root, cnt, k) { if (root === null) return -1; // Process left subtree. let left = kthSmallestRecur(root.left, cnt, k); // If kth smallest is found in left // subtree, then return it. if (left !== -1) return left; // increment node count cnt[0]++; // If curr node is kth smallest, // return it. if (cnt[0] === k) return root.data; // Else process the right subtree // and return its value. let right = kthSmallestRecur(root.right, cnt, k); return right; } // Function to find kth smallest value in BST. function kthSmallest(root, k) { let cnt = [0]; return kthSmallestRecur(root, cnt, k); } //Driver Code Starts // Driver Code // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 const root = new Node(20); root.left = new Node(8); root.right = new Node(22); root.left.left = new Node(4); root.left.right = new Node(12); root.left.right.left = new Node(10); root.left.right.right = new Node(14); const k = 3; console.log(kthSmallest(root, k)); //Driver Code Ends
We can optimize the space using Morris Traversal. Refer K’th smallest element in BST using O(1) Extra Space for details.
[Alternate Approach] Using Augmented Tree - O(h) Time and O(h) Space
The idea is to include a new data member ''lCount for each node. We can keep track of elements in the left subtree of every node while building the tree and storing it in this new data member.
Assuming that the root is having 'lCount' nodes in its left subtree. If k is equal to lCount + 1, then root is k-th node. If k is less than lCount + 1, we will continue our search (recursion) for the kth smallest element in the left subtree of root. If k is greater than lCount + 1, we continue our search in the right subtree. Note that we need the count of elements in the left subtree only.
C++ //Driver Code Starts // C++ program to find kth // smallest value in BST #include <iostream> using namespace std; class Node { public: int data; int lCount; Node* left; Node* right; Node(int x, int l) { data = x; lCount = l; left = nullptr; right = nullptr; } }; //Driver Code Ends // Recursive function for inorder traversal of the tree // and find its kth smallest value. // Returns -1 if value is not found. int kthSmallestRecur(Node* root, int &k) { if (root == nullptr) return -1; // Search left subtree if (k < root->lCount+1) { return kthSmallestRecur(root->left, k); } // return curr node else if (k == root->lCount+1) { return root->data; } // decrement k by (lCount+1) and // search right subtree else { k = k - (root->lCount+1); return kthSmallestRecur(root->right, k); } } // Function to find kth smallest value in BST. int kthSmallest(Node* root, int k) { return kthSmallestRecur(root, k); } //Driver Code Starts int main() { // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 Node* root = new Node(20, 5); root->left = new Node(8, 1); root->right = new Node(22, 0); root->left->left = new Node(4, 0); root->left->right = new Node(12, 1); root->left->right->left = new Node(10, 0); root->left->right->right = new Node(14, 0); int k = 3; cout << kthSmallest(root, k) << endl; return 0; } //Driver Code Ends
Java //Driver Code Starts // Java program to find kth // smallest value in BST class Node { int data; int lCount; Node left, right; Node(int x, int l) { data = x; lCount = l; left = null; right = null; } } class GfG { //Driver Code Ends // Recursive function for inorder traversal of the tree // and find its kth smallest value. // Returns -1 if value is not found. static int kthSmallestRecur(Node root, int[] k) { if (root == null) return -1; // Search left subtree if (k[0] < root.lCount + 1) { return kthSmallestRecur(root.left, k); } // return curr node else if (k[0] == root.lCount + 1) { return root.data; } // decrement k by (lCount+1) and // search right subtree else { k[0] = k[0] - (root.lCount + 1); return kthSmallestRecur(root.right, k); } } // Function to find kth smallest value in BST. static int kthSmallest(Node root, int k) { int[] kRef = {k}; return kthSmallestRecur(root, kRef); } //Driver Code Starts public static void main(String[] args) { // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 Node root = new Node(20, 5); root.left = new Node(8, 1); root.right = new Node(22, 0); root.left.left = new Node(4, 0); root.left.right = new Node(12, 1); root.left.right.left = new Node(10, 0); root.left.right.right = new Node(14, 0); int k = 3; System.out.println(kthSmallest(root, k)); } } //Driver Code Ends
Python #Driver Code Starts # Python program to find kth # smallest value in BST class Node: def __init__(self, x, l): self.data = x self.lCount = l self.left = None self.right = None #Driver Code Ends # Recursive function for inorder traversal of the tree # and find its kth smallest value. # Returns -1 if value is not found. def kthSmallestRecur(root, k): if root is None: return -1 # Search left subtree if k[0] < root.lCount + 1: return kthSmallestRecur(root.left, k) # return curr node elif k[0] == root.lCount + 1: return root.data # decrement k by (lCount+1) and # search right subtree else: k[0] -= (root.lCount + 1) return kthSmallestRecur(root.right, k) # Function to find kth smallest value in BST. def kthSmallest(root, k): kRef = [k] return kthSmallestRecur(root, kRef) #Driver Code Starts if __name__ == "__main__": # Binary search tree # 20 # / \ # 8 22 # / \ # 4 12 # / \ # 10 14 root = Node(20, 5) root.left = Node(8, 1) root.right = Node(22, 0) root.left.left = Node(4, 0) root.left.right = Node(12, 1) root.left.right.left = Node(10, 0) root.left.right.right = Node(14, 0) k = 3 print(kthSmallest(root, k)) #Driver Code Ends
C# //Driver Code Starts // C# program to find kth // smallest value in BST using System; class Node { public int data; public int lCount; public Node left, right; public Node(int x, int l) { data = x; lCount = l; left = null; right = null; } } class GfG { //Driver Code Ends // Recursive function for inorder traversal of the tree // and find its kth smallest value. // Returns -1 if value is not found. static int kthSmallestRecur(Node root, ref int k) { if (root == null) return -1; // Search left subtree if (k < root.lCount + 1) { return kthSmallestRecur(root.left, ref k); } // return curr node else if (k == root.lCount + 1) { return root.data; } // decrement k by (lCount+1) and // search right subtree else { k -= (root.lCount + 1); return kthSmallestRecur(root.right, ref k); } } // Function to find kth smallest value in BST. static int kthSmallest(Node root, int k) { return kthSmallestRecur(root, ref k); } //Driver Code Starts static void Main(string[] args) { // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 Node root = new Node(20, 5); root.left = new Node(8, 1); root.right = new Node(22, 0); root.left.left = new Node(4, 0); root.left.right = new Node(12, 1); root.left.right.left = new Node(10, 0); root.left.right.right = new Node(14, 0); int k = 3; Console.WriteLine(kthSmallest(root, k)); } } //Driver Code Ends
JavaScript //Driver Code Starts // JavaScript program to find kth // smallest value in BST class Node { constructor(x, l) { this.data = x; this.lCount = l; this.left = null; this.right = null; } } //Driver Code Ends // Recursive function for inorder traversal of the tree // and find its kth smallest value. // Returns -1 if value is not found. function kthSmallestRecur(root, k) { if (root === null) return -1; // Search left subtree if (k[0] < root.lCount + 1) { return kthSmallestRecur(root.left, k); } // return curr node else if (k[0] === root.lCount + 1) { return root.data; } // decrement k by (lCount+1) and // search right subtree else { k[0] -= (root.lCount + 1); return kthSmallestRecur(root.right, k); } } // Function to find kth smallest value in BST. function kthSmallest(root, k) { let kRef = [k]; return kthSmallestRecur(root, kRef); } //Driver Code Starts // Driver Code // Binary search tree // 20 // / \ // 8 22 // / \ // 4 12 // / \ // 10 14 const root = new Node(20, 5); root.left = new Node(8, 1); root.right = new Node(22, 0); root.left.left = new Node(4, 0); root.left.right = new Node(12, 1); root.left.right.left = new Node(10, 0); root.left.right.right = new Node(14, 0); const k = 3; console.log(kthSmallest(root, k)); //Driver Code Ends
Similar Reads
Binary Search Tree A Binary Search Tree (BST) is a type of binary tree data structure in which each node contains a unique key and satisfies a specific ordering property:All nodes in the left subtree of a node contain values strictly less than the nodeâs value. All nodes in the right subtree of a node contain values s
4 min read
Introduction to Binary Search Tree Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the
3 min read
Applications of BST Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations along with maintaining sorted sequence of data. Please remember the following properties of BSTs before moving forward.The left subtree of a node contains only node
3 min read
Applications, Advantages and Disadvantages of Binary Search Tree A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the p
2 min read
Insertion in Binary Search Tree (BST) Given a BST, the task is to insert a new node in this BST.Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is fo
15 min read
Searching in Binary Search Tree (BST) Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp
7 min read
Deletion in Binary Search Tree (BST) Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios:Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BSTDeleting a single child node is also simple in BST. Copy the child to the node and delete the node. Case 3. Delete a Node w
10 min read
Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: A Binary Search TreeOutput: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 1
10 min read
Balance a Binary Search Tree Given a BST (Binary Search Tree) that may be unbalanced, the task is to convert it into a balanced BST that has the minimum possible height.Examples: Input: Output: Explanation: The above unbalanced BST is converted to balanced with the minimum possible height.Input: Output: Explanation: The above u
10 min read
Self-Balancing Binary Search Trees Self-Balancing Binary Search Trees are height-balanced binary search trees that automatically keep the height as small as possible when insertion and deletion operations are performed on the tree. The height is typically maintained in order of logN so that all operations take O(logN) time on average
4 min read