Given a Perfect BST with N nodes and an integer K, the task is to find the Kth smallest element in present in the tree.
Input: K = 3, N = 15 50 / \ 30 70 / \ / \ 20 40 60 80 /\ /\ /\ / \ 14 25 35 45 55 65 75 85 Output: 25 Explanation: The 3rd smallest element in the given BST is 25 Input: K = 9, N = 15 50 / \ 30 70 / \ / \ 20 40 60 80 /\ /\ /\ / \ 14 25 35 45 55 65 75 85 Output: 55 Explanation: The 9th smallest element in the given BST is 55
Naive Approach: Do the Inorder traversal in a perfect BST, such as morris traversal or recursive solution, which visits every node and return the kth visit key. It takes O(N) time complexity to complete the task.
Since the given BST is perfect and the number of nodes of the entire tree is already known, the computational complexity to solve the problem can be reduced to log(N). Follow the steps given below to solve the problem:
- Calculate the median of every sub-tree by dividing the total nodes in the tree(N) as |N| / 2.
- The left sub-tree of the Perfect BST will always contain nodes with smaller value than K nodes if :
- K < location(median(N))
- The right sub-tree of Perfect BST will always contain nodes with greater value than K nodes if :
- K > location(median(N)), in this case, Kth smallest element for right sub-tree is K – location(median(N)).
- Since |N| will always be odd in a perfect binary tree, the location of the median in any perfect BST is |N|/2 + 1.
- If K is location(median(T)), then that node contains the Kth smallest element in perfect BST.
Below is the implementation of the above approach:
Time complexity: O(Log(N))
Auxiliary Space: O(Log(N))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Meta Binary Search | One-Sided Binary Search
- Floor value Kth root of a number using Recursive Binary Search
- Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Check if a given number is a Perfect square using Binary Search
- Repeatedly search an element by doubling it after every successful search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- kth smallest/largest in a small range unsorted array
- Special two digit numbers in a Binary Search Tree
- Floor in Binary Search Tree (BST)
- Double Threaded Binary Search Tree
- Binary Search Tree | Set 3 (Iterative Delete)
- Find K smallest leaf nodes from a given Binary Tree
- Best First Search (Informed Search)
- Sublist Search (Search a linked list in another list)
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Find maximum sum taking every Kth element in the array
- Find the kth element in the series generated by the given N ranges
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.