You are given the root of a Binary Search Tree (BST) and an integer k. Your task is to find the kth largest value among all node values in the tree.
Return the kth largest element present in the BST.
Input: root = [5,3,8,1,4,null,9], k = 2
Output: 8
Explanation: In descending order the values are 9, 8, 5, 4, 3, 1. The 2nd largest value is 8.
Input: root = [5,3,8,1,4,null,9], k = 1
Output: 9
Explanation: The largest value in the BST is 9.
Input: root = [42], k = 1
Output: 42
Explanation: The only value is both the largest and the 1st largest element.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
50 30 70 20 40 60 80 3
60
40 20 60 10 30 50 70 5
30
80 50 100 30 60 90 120 2
100