Given a BST and a positive number k, find the k'th largest node in the BST.

For example, consider the following binary search tree. If k = 2, the k'th largest node is 20.

 
K'th largest node in the BST

Practice this problem

We know that an inorder traversal of a binary search tree returns the nodes in ascending order. To find the k'th smallest node, we can perform inorder traversal and store the inorder sequence in an array. Then the k'th largest node would be the (n-k)'th smallest node, where n is the total number of nodes present in the BST.

The problem with this approach is that it requires two traversals of the array. We can solve this problem in a single traversal of the array by using reverse inorder traversal (traverse the right subtree before the left subtree for every node). Then the reverse inorder traversal of a binary search tree will process the nodes in descending order.

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

20

Java


Download  Run Code

Output:

20

Python


Download  Run Code

Output:

20

The time complexity of the above solution is O(n), where n is the size of the BST, and requires space proportional to the tree’s height for the call stack.