Skip to content

Instantly share code, notes, and snippets.

@iamprayush
Created September 19, 2020 14:11
Show Gist options
  • Save iamprayush/1e46d701b6ac223bdd1847b02f019d43 to your computer and use it in GitHub Desktop.
Save iamprayush/1e46d701b6ac223bdd1847b02f019d43 to your computer and use it in GitHub Desktop.
Kth Smallest Element in a BST
class Solution:
def kthSmallest(self, root: TreeNode, k: int) -> int:
stack = []
curr = root
while curr or stack:
if curr:
stack.append(curr)
curr = curr.left
else:
curr = stack.pop()
k -= 1
if k == 0:
return curr.val
curr = curr.right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment