Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Kth Smallest Element in a BST

XPChallenge Points: 20
levelLevel: Medium

You are given the root of a binary search tree (BST) and an integer k.
Return the kth smallest value in the tree (counting starts from 1).

Example 1:

Input: root = [4,2,6,1,3,5,7], k = 4

Output: 4

Explanation:

The tree’s values in sorted order are [1,2,3,4,5,6,7]. The 4th smallest number is 4.

Example 2:

Input: root = [2,1,3], k = 2

Output: 2

Explanation:

Sorted order is [1,2,3]. The 2nd smallest number is 2.

Example 3:

Input: root = [7,3,9,2,5,8,10], k = 5

Output: 8

Explanation:

Sorted order is [2,3,5,7,8,9,10]. The 5th smallest number is 8.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result