Kth Smallest Element in a BST
Find the kth smallest value in a BST using an iterative stack with early termination.
01The problem
Given the root of a binary search tree, and an integer k, return the k-th smallest value (1-indexed) of all the values of the nodes in the tree.
text
5
/ 3 6
/ 2 4
/
1
k = 3
Inorder: [1, 2, 3, 4, 5, 6]
3rd smallest: 3
Output: 3Constraints
- The number of nodes in the tree is
n. 1 <= k <= n <= 10^40 <= Node.val <= 10^4
Loading the rest of the lesson…