Medium · Trees

Find Kth largest in BST

Given the root of a binary search tree with distinct values and an integer k with 1 ≤ k ≤ the number of nodes, return the kth largest value in the tree.

Examples

Example 1

[5,3,8,2,4,7,9], k=2

Output: 2nd largest = 8

Example 2

[4,2,6,1,3,5,7], k=3

Output: 3rd largest = 5

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems