183. Kth Smallest Element in a BST

MediumTree

Given the `root` of a binary search tree and an integer `k`, return the kth smallest value (1-indexed) among all the node values in the tree. The tree is given as a level-order array. For example, given root = [3,1,4,null,2] and k = 1, the sorted values are [1,2,3,4], so the 1st smallest is 1.

Examples

Input: [3,1,4,null,2] 1

Output: 1

Explanation: In-order traversal gives [1,2,3,4]. The 1st smallest is 1.

Constraints

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

Run checks all cases above. Submit evaluates all test cases.