Dsa
Binary Search Tree
Easy
Find Kth Smallest Element in BST
You are given a binary search tree (BST) and an integer K. Your task is to find the Kth smallest element in the BST.
Implement the following functions:
1. `insert(root: Optional[Node], value: int) -> Node`: Insert a value into the BST.
2. `kth_smallest(root: Optional[Node], k: int) -> int`: Return the Kth smallest element in the BST.
Input format:
- The first line contains an integer N (1 <= N <= 100) - the number of elements in the BST.
- The next N lines each contain one integer, the value to be inserted into the BST.
- The last line contains an integer K (1 <= K <= N).
Output format:
- Output the Kth smallest element in the BST.
Example:
Input:
5
7
3
9
1
5
3
Output:
5
Key concepts
binary_search_treekth_elementinorder_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