You are given the root of a Binary Search Tree (BST) and an integer k. Your task is to return the kth smallest element in the BST.
Input: root = [3,1,4,null,2], k = 1
Output: 1
Explanation: The smallest element in the BST is 1.
Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3
Explanation: The elements in sorted order are 1, 2, 3, 4, 5, 6, so the 3rd smallest element is 3.
Input: root = [2,1,3], k = 2
Output: 2
Explanation: The 2nd smallest element in the BST is 2.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
50 30 70 20 40 60 80 3
40
40 20 60 10 30 50 70 5
50
80 50 100 30 60 90 120 2
50