📘
Kth Smallest Element in a BST
MediumTrees
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,2represents a Binary Search Tree (BST) where the parent node is3, the left child is1, the right child is4, and the right child of the left child is2. - The BST is traversed in-order to obtain the sorted array:
1, 2, 3, 4. - Since
kis1, we need to find the 1st smallest element in the sorted array. - The 1st smallest element is
1, which is the final output.
Constraints:
- 1 <= k <= number of nodes <= 10^4
- 0 <= Node.val <= 10^4
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.