v9.1.1
Menu

Kth Smallest Element in a BST

mediumFREETreesBlind 75

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

Example

Input
3,1,4,null,2
1
Output
1
Reasoning
  • The given level-order array 3,1,4,null,2 represents a Binary Search Tree (BST) where the parent node is 3, the left child is 1, the right child is 4, and the right child of the left child is 2.
  • The BST is traversed in-order to obtain the sorted array: 1, 2, 3, 4.
  • Since k is 1, we need to find the 1st1^{st} smallest element in the sorted array.
  • The 1st1^{st} smallest element is 1, which is the final output.

Constraints

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

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.