Advertisement
Guest User

Kth Smallest Node in BST

a guest
Mar 8th, 2021
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  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
Add Comment
Please, Sign In to add comment
Advertisement