InferenceAI

Enter at least two characters to search.

Start free

Kth Smallest in a BST

mediumTrees25 min25 XP

Given a valid binary search tree, return its k-th smallest value (1-indexed).

Trees are given in level order as a list, with None for a missing child (for example [3, 9, 20, None, None, 15, 7]).

Examples

EXAMPLE WALKTHROUGHsmallest

A guided view of the published example, not a trace of your code.

INPUT[[3,1,4,null,2],1]
TASKkth_smallest_in_bst()
EXPECTED OUTPUT1

Start with the input values shown in this example.

Example 1 of 3

Input: values = [3,1,4,null,2], arg2 = 1

Output: 1

Explanation: smallest

Input: values = [5,3,6,2,4,null,null,1], arg2 = 3

Output: 3

Explanation: third

Input: values = [1], arg2 = 1

Output: 1

Explanation: single

Hint 1

In-order traversal of a BST visits values in sorted order.

Hint 2

Stop as soon as you've visited k nodes.

Topics

DSATreesPython