Kth LARGEST ELEMENT IN BST

 Find the Kth largest element in bst.

Approach 1: Use inorder traversal to store the tree elements in an array. The array will be in sorted order.

    Element at index (n - k) will be answer. Where n is the length of the array.


time - O(n)

space - O(n)

Solution - kth largest element using inorder 

Approach 2 : Reverse Inorder Technique

  1. We will create a class node which stores value(we will store the tree node values there ), counter(ctr), it store the count of variable.
  2. Now we will create nde of class node that keep track of value and kth index and pass it to the helper function.


3. We will traverse right of bst and then to left. And we will check the condition if we are at the kth node.

Also we will store the value of node we visit.



Time - O(h + k)
space - O(h)
where h is the height of the tree




Please leave comment for any suggestions or topic you would like to read.


Comments