
Leetcode Problem 230: Kth Smallest Element in a BST
Given the root of a Binary Search Tree and an integer k, return the k-th smallest value among all the node values. k = 1 means the smallest value.
Constraints:
k is a valid integer between 1 and the number of nodes.export class TreeNode {
val: number
left: TreeNode | null
right: TreeNode | null
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = (val === undefined ? 0 : val)
this.left = (left === undefined ? null : left)
this.right = (right === undefined ? null : right)
}
}
function kthSmallest(root: TreeNode | null, k: number): number {
let stack: TreeNode[] = []
let current = root
let currentIndex = 1
while (current || stack.length > 0) {
while (current) {
stack.push(current)
current = current.left
}
current = stack.pop()!
if (currentIndex++ === k) {
return current.val
}
current = current.right
}
return -1
}