"""
230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

Note: 
You may assume k is always valid, 1 <= k <= BST's total elements.

Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

"""

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

# MOWN
# inorder travesal and decrement k each time we print a node, when k is 0 we found kth smallest

class Solution(object):
    def kthSmallest(self, root, k):
        """
        :type root: TreeNode
        :type k: int
        :rtype: int
        """
        stack = []

        troot = root
        while True:
            if troot:
                stack.append(troot)
                troot = troot.left

            else:
                if stack:
                    troot = stack.pop()
                    k -= 1
                    if not k:
                        return troot.val

                    troot = troot.right
                else:
                    break
        return None