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
- 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.
- Now we will create nde of class node that keep track of value and kth index and pass it to the helper function.
Also we will store the value of node we visit.
Please leave comment for any suggestions or topic you would like to read.


Comments
Post a Comment
If you have any doubt, Please let me know