Given a binary search tree (BST), write a function to find the Kth smallest element in the tree.
class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def kth_smallest_element(root, k):
# Your code here
def kth_smallest_element(root, k):
stack = []
curr = root
while True:
while curr is not None:
stack.append(curr)
curr = curr.left
curr = stack.pop()
k -= 1
if k == 0:
return curr.value
curr = curr.right