885. Kth Smallest Element in BST

EasyTreesBSTInorder

Given the root of a binary search tree and an integer k (1-indexed), return the k-th smallest key in the BST. The input gives a level-order BST array and k separated by ' | '.

Input: A level-order BST array and k, separated by ' | '.

Output: Integer — the k-th smallest key.

Examples

Example 1
Input: [8,3,10,1,6,null,14] | 3
Output: 6
Explanation: Sorted keys; 3rd smallest is 6.
Example 2
Input: [4,2,7,1,3] | 1
Output: 1
Explanation: Smallest key.
Example 3
Input: [5] | 1
Output: 5
Explanation: Single node.

Constraints

Asked by

OracleBloombergAmazonMicrosoftGoogleMeta
Solve this problem in the editor →