Created
September 19, 2020 14:11
-
-
Save iamprayush/1e46d701b6ac223bdd1847b02f019d43 to your computer and use it in GitHub Desktop.
Kth Smallest Element in a BST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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