Kth Smallest Element in a BST
Beginner Mode

Problem Statement

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Additional information

  • The number of nodes in the tree is n.
  • 1 <= k <= n <= 10^4
  • 0 <= Node.val <= 10^4

Example 1:

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

Output: 1

Example 2:

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

Output: 3

Explanation: The inorder traversal of the tree is [1, 2, 3, 4, 5, 6]. The 3rd smallest element is 3.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →