#230
Kth Smallest Element in a BST
medium· Treesruns: 0Given the root of a binary search tree and an integer k, return the kth smallest value (1-indexed) among all values in the tree.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 394
class Solution:
def kthSmallest(self, root: TreeNode | None, k: int) -> int:
stack = []
curr = root
while curr or stack:
while curr:
stack.append(curr)
curr = curr.left
curr = stack.pop()
k -= 1
if k == 0:
return curr.val
curr = curr.right
return -1
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.