Tree DFS
LC #230Medium
Kth Smallest Element in a BST
Tree DFS
AmazonBloombergGoogleMetaProblem
Find the kth smallest element in a BST.
treedfsbst
Constraints
- ›1 ≤ k ≤ n ≤ 10⁴
- ›0 ≤ node.val ≤ 10⁴
Example
Input
root = [3,1,4,null,2], k = 1Output
1Why
In-order traversal gives [1, 2, 3, 4]. The 1st smallest is 1.