algorythms
Tree DFS
LC #230Medium

Kth Smallest Element in a BST

Tree DFS
AmazonBloombergGoogleMeta

Problem

Find the kth smallest element in a BST.

treedfsbst

Constraints

  • 1 ≤ k ≤ n ≤ 10⁴
  • 0 ≤ node.val ≤ 10⁴

Example

Inputroot = [3,1,4,null,2], k = 1
Output1
Why

In-order traversal gives [1, 2, 3, 4]. The 1st smallest is 1.

Hints — reveal one at a time