Challenge 2: Finding kth Maximum Value in Binary Search Tree
Given the root to a binary search tree and a number "k," write a function to find the “kth” maximum value in the tree.
We'll cover the following
Problem statement
Implement a function int findKthMax(Node* rootNode, int k,int nodes)
, which will take the root of a BST, any number “k” as an input, and the total number of nodes in the tree. It will return the “kth” maximum number from that tree.
BST cannot have duplicate node values.
Input
The input is the root node, the value k, and the total nodes in a BST.
Output
It returns the “kth” maximum value from the given tree.
Sample input
bst = {
6 -> 4,9
4 -> 2,5
9 -> 8,12
12 -> 10,14
}
where parent -> leftChild,rightChild
k = 3
Total nodes = 9
Sample output
10
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.