Challenge: Find the kth Maximum Value in a 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.

Problem Statement

Implement a function findKthMax(rootNode,k) which will take a BST and any number “k” as an input and return kth maximum number from that tree. Assume that “k” will always be less than or equal to the total number of nodes.

Input

The root node for a binary search tree and any number k

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.