Data Structures19 sections · 729 units
Open in Course

Problem - Kth Smallest Element

Use inorder property

Given the root of a BST and an integer k, return the kth smallest value (1-indexed) of all the values in the tree.

Example: In BST [3,1,4,null,2][3,1,4,null,2], the 1st smallest is 1, 2nd is 2, 3rd is 3. This problem directly tests whether you understand the inorder property of BSTs.

Follow-up: If the BST is modified often (insert/delete) and you need to find kth smallest frequently, how would you optimize? Constraints: kk is in range [1,n][1, n], up to 10410^4 nodes.