Find Kth largest in a BST - Given a BST and a number k, find the kth largest element in the BST.
Input: Tree
[10]
/ \
[5] [15]
/ \ / \
[1] [7] [11] [18]
K = 3
Output: 11
Node with the value of 11
is the Third Largest element in the tree.
You're given a method to insert values in a BST. The driver code will do that for you.
class BST {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
Click to reveal
Recursion is your best friend. :)
Click to reveal
There are various ways to traverse a BST. Inorder, Preorder, and Postorder are the common ways. Which one of them returns a sorted order? Can sorting help you here?
Click to reveal
Can you try maintaining a separate class to store information while you're traversing the given tree? Do you need to sort the entire tree first and then compute the largest? Can it be done parallelly?