Facebook Pixel

K-th Smallest Number In BST

Given the root node of a valid BST and a number k, return the kth smallest number in this BST (1-indexed).

Input & Output
Input
bst — a binary tree representing the existing BST.
k — an integer.
Output
The `k`th smallest number in `bst`.
Example
Input
bst = <See explanation>
k = 4
Output
6
Explanation

Constraints
  • 1 <= k <= n <= 10^5, where n is the size of bst.

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro