Find 3rd smallest
Input
root = build_bst([5,3,7,2,4,6,8]); k = 3 result = kth_smallest(root, 3)
Output
4
Inorder traversal gives [2,3,4,5,6,7,8]; the 3rd element is 4.
Full lesson preview
Return the kth smallest value in a BST using an efficient in-order traversal.
Problem statement
Task
Examples
Input
root = build_bst([5,3,7,2,4,6,8]); k = 3 result = kth_smallest(root, 3)
Output
4
Inorder traversal gives [2,3,4,5,6,7,8]; the 3rd element is 4.
Input format
Output format
Constraints
Samples
Input
kth_smallest(build_bst([5,3,7,2,4,6,8]), 1)
Output
2
Smallest element is 2.