Dsa
Binary Search Tree
Hard
Find K-th Smallest Element in BST
You are given the root node of a binary search tree (BST) and an integer k. Your task is to find the k-th smallest element in the BST. The smallest element is considered to be the first one. If k exceeds the number of nodes in the tree, return -1.
Input format:
- The first line contains an integer n, the number of nodes in the BST.
- The following n lines contain three integers each: the first integer is the node value, and the next two integers are the indices of the left and right children (or -1 if no child).
- The last line contains an integer k.
Output format:
- Print the k-th smallest element or -1 if it does not exist.
Example:
Input:
3
5 1 2
3 -1 -1
7 -1 -1
2
Output:
5
Key concepts
binary_search_treetraversalsearch
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