VOOZH about

URL: https://www.geeksforgeeks.org/dsa/kth-largest-element-in-bst-when-modification-to-bst-is-not-allowed/

⇱ K'th Largest Element in BST when modification to BST is not allowed - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

K'th Largest Element in BST when modification to BST is not allowed

Last Updated : 23 Jul, 2025

Given a Binary Search Tree (BST) and a positive integer k, the task is to find the kth largest element in the Binary Search Tree.

Example:

Input: k = 3

πŸ‘ th-Largest-element-in-BST

Output: 14
Explanation: If we sort the BST in decreasing order, then it will become 22, 20, 14, 12, 10, 8, 4. 14 is the 3rd largest element.

Using Recursion - O(n) Time and O(h) Space

The idea is to traverse the binary search tree in reverse in-order manner. This way we will traverse the elements in decreasing order. Maintain the count of nodes traversed so far. If count becomes equal to k, return the element.


Output
14

Using Stack - O(n) Time and O(h) Space

The idea is to similar to recursion, but in this method, stack will be used to traverse the binary search tree in reverse in-order manner. This way we will traverse the elements in decreasing order. Maintain the count of nodes traversed so far. If count becomes equal to k, return the element.

Below is the implementation of the above approach:


Output
14

Using Morris Traversal Algorithm - O(n) Time and O(1) Space

The idea is to use Reverse Morris Traversal Algorithm to traverse the binary search tree in reverse in-order manner and maintain the count of nodes traversed so far. If number of nodes traversed become equal to k, then return the node. Please refer to K’th Largest element in BST using constant extra space for implementation.

Comment