Advertisement
Guest User

Kth Smallest Node in BST

a guest
Mar 8th, 2021
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def kthSmallest(root: TreeNode, k: int) -> int:
  2.     cur = root
  3.     stack = []
  4.     counter = 1
  5.     while cur or len(stack):
  6.         while cur:
  7.             stack.append(cur)
  8.             cur = cur.left
  9.         cur = stack.pop()
  10.         if counter == k:
  11.             return cur.val
  12.         counter += 1
  13.         cur = cur.right
  14.  
  15.     return None
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement