Kth Smallest Element in BST
Tree
medium
Score: 40
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.
Class TreeNode:
val (int)
left (TreeNode)
right (TreeNode)
Input Format:
First Parameter - TreeNode root
Second Parameter - Number k
Output Format:
Return the number.
Example 1:
Input:
3 1 4 null 2
1
Output:
1
Example 2:
Input:
5 3 6 2 4 null null 1
3
Output:
3
Constraints:
- The number of nodes in the tree is
n
- 1 <=
k
<=n
<= 104 - 0 <=
node.val
<= 104 - Expected Time Complexity: O(N)
- Expected SpaceComplexity: O(N)