Kth Smallest Element in a BST Problem
Kth Smallest Element in a BST Problem — ExecCode Medium DSA Practice
Solve the Kth Smallest Element in a BST problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given the root of a binary search tree and an integer k, return the kth smallest value (1-indexed) among all the nodes in the tree. Because an inorder traversal of a BST visits values in ascending order, the answer is simply the kth value produced by that traversal.
Examples
Input root = [5, 3, 6, 2, 4, null, 7], k = 3; Output 4. Input root = [3, 1, 4, null, 2], k = 1; Output 1. Input root = [5, 3, 6, 2, 4, null, 7], k = 6; Output 7
Constraints
1 <= k <= n <= 10^4 -10^4 <= Node.val <= 10^4
Practice Kth Smallest Element in a BST free on ExecCode. Browse DSA problems, topic map, and placement guides.