Given a binary search tree, task is to find Kth largest element in the binary search tree.
Input : k = 3 Root of following BST 10 / \ 4 20 / / \ 2 15 40 Output : 15
The idea is to use Reverse Morris Traversal which is based on Threaded Binary Trees. Threaded binary trees use the NULL pointers to store the successor and predecessor information which helps us to utilize the wasted memory by those NULL pointers.
The special thing about Morris traversal is that we can do Inorder traversal without using stack or recursion which saves us memory consumed by stack or recursion call stack.
Reverse Morris traversal is just the reverse of Morris traversal which is majorly used to do Reverse Inorder traversal with constant O(1) extra memory consumed as it does not uses any Stack or Recursion.
To find Kth largest element in a Binary search tree, the simplest logic is to do reverse inorder traversal and while doing reverse inorder traversal simply keep a count of number of Nodes visited. When the count becomes equal to k, we stop the traversal and print the data. It uses the fact that reverse inorder traversal will give us a list sorted in descending order.
1) Initialize Current as root. 2) Initialize a count variable to 0. 3) While current is not NULL : 3.1) If current has no right child a) Increment count and check if count is equal to K. 1) If count is equal to K, simply return current Node as it is the Kth largest Node. b) Otherwise, Move to the left child of current. 3.2) Else, here we have 2 cases: a) Find the inorder successor of current Node. Inorder successor is the left most Node in the right subtree or right child itself. b) If the left child of the inorder successor is NULL: 1) Set current as the left child of its inorder successor. 2) Move current Node to its right. c) Else, if the threaded link between the current Node and it's inorder successor already exists : 1) Set left pointer of the inorder successor as NULL. 2) Increment count and check if count is equal to K. a) If count is equal to K, simply return current Node as it is the Kth largest Node. 3) Otherwise, Move current to it's left child.
Finding K-th largest Node in BST : 7
Time Complexity : O(n)
Auxiliary Space : O(1)
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.
- K'th smallest element in BST using O(1) Extra Space
- Merge two BSTs with constant extra space
- K'th Largest Element in BST when modification to BST is not allowed
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Find k-th smallest element in BST (Order Statistics in BST)
- k smallest elements in same order using O(1) extra space
- Merge two BSTs with limited extra space
- Two nodes of a BST are swapped, correct the BST | Set-2
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST
- Second largest element in BST
- Find median of BST in O(n) time and O(1) space
- Print BST keys in given Range | O(1) Space
- Largest element smaller than current element on left for every element in Array
- Find the largest BST subtree in a given Binary Tree | Set 1
- Largest BST in a Binary Tree | Set 2
- Sum of k largest elements in BST
- Largest number in BST which is less than or equal to N
- Largest number less than or equal to N in BST (Iterative Approach)
- Maximum element between two nodes of BST
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.