Challenge: Find 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. A solution is placed in the "solution" section for your help, but we would suggest you solve it on your own first.
We'll cover the following
Problem Statement
Implement a function int findKthMax(Node* rootNode, int k)
, which will take the root of a BST and any number “k” as an input. It will return kth maximum number from that tree.
BST cannot have duplicate node values.
Output
Returns 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
Sample Output
10
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.