230. Kth Smallest Element in a BST
https://leetcode.com/problems/kth-smallest-element-in-a-bst/
class Solution:
def kthSmallest(self, root: TreeNode, k: int) -> int:
cnt = 0
stack = []
curr = root
stack.append(curr)
while stack:
while curr.left:
stack.append(curr.left)
curr = curr.left
node = stack.pop()
cnt+=1
if cnt == k:
return node.val
node = node.right
while node:
stack.append(node)
node = node.left
return -1Last updated