Kth Smallest Element in BST

Problem

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

For example:

Input: root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
   2
Output: 1
Input: root = [5,3,6,2,4,null,null,1], k = 3
       5
      / \
     3   6
    / \
   2   4
  /
 1
Output: 3

Solution

Last updated

Was this helpful?