View on GitHub

leetcode-diaries

A journal to record the solution of the various Leetcode problems solved over the course of time.

Kth Smallest Element in a BST

Leetcode problem 230; Difficulty: Medium

Problem Statement

Given the root of a binary search tree, and an integer k, return the kth (1-indexed) smallest element in the tree.

Constraints:

Example 1:

image

Input: root = [3,1,4,null,2], k = 1

Output: 1

Example 2:

image

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

Output: 3

Follow up:

If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize? (To be implemented)

Submitted Solution

The implemented solution recevied the following rating on Leetcode:

image