PIXELBANKv9.1.0
Menu

Kth Smallest Element in BST

Given the root of a BST (as a level-order array) and an integer k, return the kth smallest value (1-indexed) in the tree.

Example:

Input:
3,1,4,null,2
1
Output:
1
Reasoning:
  • The given level-order array represents a binary search tree (BST) where the root node is 3, its left child is 1, its right child is 4, and the left child of the right child (4) is 2.
  • The BST is traversed in-order to get the nodes in ascending order: 1, 2, 3, 4.
  • Since k=1k = 1, we need to find the 1st smallest element in the sorted list.
  • The 1st smallest element in the sorted list is 1, so the output is 1.

Constraints:

  • 1 <= k <= number of nodes <= 10^4
  • 0 <= Node.val <= 10^4
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Kth Smallest Element in BST - Medium | PixelBank