Faaez Razeen

Kth Smallest Element in Binary Search Tree

  • 1 min read
  • LC-Medium
  • Binary-search-tree

3 years ago

Solution

TimeSpaceExplanation
O()O()
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: cur = root stack = [] while stack or cur: while cur: stack.append(cur) cur = cur.left node = stack.pop() k -= 1 if k == 0: return node.val cur = node.right