Dsa
Binary Search Tree
Medium
The Leaf-Pruned K-th
Build a BST from a sequence of unique integers. A 'Pruning' operation removes all nodes that are currently leaves (nodes with no children). Perform one round of pruning, and then find the K-th smallest element in the remaining tree. If the tree becomes empty or has fewer than K nodes, output -1.
Input Format:
- Line 1: Space-separated integers for the BST.
- Line 2: The integer K.
Output Format:
- The K-th smallest integer or -1.
Example:
Input:
10 5 15 2 7 12 18
2
Output:
10
Explanation: Initial leaves: 2, 7, 12, 18. Remaining nodes: 10, 5, 15. Sorted: 5, 10, 15. 2nd smallest is 10.
Key concepts
binary_search_treetree_modificationinorder_traversal
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