Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Kth Smallest Element in BST

Medium Acceptance 76.77% Points 30.00

You are given the root of a Binary Search Tree (BST) and an integer k. Your task is to return the kth smallest element in the BST.

Examples
Example 1

image Input: root = [3,1,4,null,2], k = 1
Output: 1
Explanation: The smallest element in the BST is 1.

Example 2

image Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3
Explanation: The elements in sorted order are 1, 2, 3, 4, 5, 6, so the 3rd smallest element is 3.

Example 3

image Input: root = [2,1,3], k = 2
Output: 2
Explanation: The 2nd smallest element in the BST is 2.

Constraints
    • 1 = Number of nodes = 105
    • -10? = Node.data = 10?
    • 1 = k = Number of nodes
Companies
Expedia Morgan Stanly
Topics
Tree Traversal Binary Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions