Skip to interview prep
Apexed

91. Kth Smallest Value in a BST

MediumTrees

Return the k-th smallest value (1-based) in a binary search tree.

A binary tree is passed as TreeNode objects with val, left and right. The tests write trees in level order, with null for a missing child.

Example 1

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

Constraints

  • 1 ≤ k ≤ nodes ≤ 10⁴
  • The tree is a valid BST with distinct values

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Kth Smallest Value in a BST — coding interview practice — Apexed