K-th Largest Number in a BST
Given a binary search tree (BST) consisting of integers and containing 'N' nodes, your task is to find and return the K-th largest element in this BST.
If there is no K-th largest element in the BST, return -1.
Example:
Input:
1
K: 3
BST elements (level-order): 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
5
Explanation:
For the binary search tree created from the input, the in-order traversal gives the sequence [1, 2, 3, 4, 5, 6, 7]. The 3rd largest element is 5.
Constraints:
1 <= T <= 100
1 <= N <= 5000
1 <= K <= 5000
0 <= Data <= 10^6
andData ≠ -1
- Time Limit: 1 second
Note:
You do not have to print anything; only implement the function to return the desired result.
Be the first one to answer
Add answer anonymously...
Top Freshworks Senior Software Engineer interview questions & answers
Popular interview questions of Senior Software Engineer
Top HR questions asked in Freshworks Senior Software Engineer
>
Freshworks Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app