Dsa
Trees
Medium
Kth Largest Element in a Tree
Given a binary search tree (BST), write a function to find the Kth largest value in the tree. The Kth largest is defined as the value that would be in the Kth position if all values are sorted in descending order. The tree will be provided in level-order format (using '#' for null nodes).
Input format:
- The first line contains a space-separated list of values representing the BST in level-order.
- The second line contains a single integer K, representing the position to find.
Output format:
- A single integer representing the Kth largest value.
Example:
Input:
5 3 8 1 4 7 9
3
Output:
7
Key concepts
treesorder statisticsdepth-first search
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now