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.

Example 1:


Input: root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
   2
Output: 1

Example 2:


Input: root = [5,3,6,2,4,null,null,1], k = 3
       5
      / \
     3   6
    / \
   2   4
  /
 1
Output: 3


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?

Difficulty: interview

Starter code:

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
    self.val = val
    self.left = left
    self.right = right
class Solution:
    def kthSmallest(self, root: TreeNode, k: int) -> int:
        

Solutions:
class Solution:
     def __init__(self):
         self.k = 0
         self.result = None
 
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         if root.left and self.k < k:
             self.kthSmallest(root.left, k)
         self.k += 1
         if self.k == k:
             self.result = root.val
         if root.right and self.k < k:
             self.kthSmallest(root.right, k)
         return self.result

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def find(self, root, k):
         if root is None:
             return (None, 0)
         else:
             (v, num), rnum = self.find(root.left, k), 0
             if v is None:
                 v, num = (root.val, 0) if num == k - 1 else (None, num)
                 if v is None:
                     v, rnum = self.find(root.right, k - num - 1)
             return (v, num + rnum + 1)
                 
     
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         return self.find(root, k)[0]

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         if root == None:
             return None
         else:
             node = root
             visited = []
             count = 0
             while node or visited:
                 if node:
                     visited.append(node)
                     node = node.left
                 else:
                     cur = visited.pop()
                     count = count+1
                     if count == k:
                         return cur.val
                     else:
                         node = cur.right
             return None
         

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         cur = root
         stack = []
         count = 0
         while(len(stack) > 0 or cur):
             while(cur):
                 stack.append(cur)
                 cur = cur.left
             tmp = stack.pop()
             count += 1
             if(count == k): return tmp.val
             cur = tmp.right
         return -1

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def helper(self,node):
         if node is not None:
             self.helper(node.left)
             self.stack.append(node.val)
             self.helper(node.right)
         
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         self.stack = []
         if root is not None:
             self.helper(root)
             return self.stack[k-1]
         else:
             return None
         

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         res=[];
         self.kthRec(root,res);
         return res[k-1];
     
     def kthRec(self,root,res):
         if(root!=None):
             self.kthRec(root.left,res);
             res.append(root.val);
             self.kthRec(root.right,res);
             

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         if not root:
             return 0
         
         distanceToLeft = self.dfs(root.left)
         if distanceToLeft + 1 == k:
             return root.val
         elif distanceToLeft + 1< k:
             return self.kthSmallest(root.right, k - distanceToLeft - 1)
         else:
             return self.kthSmallest(root.left, k)
     
     def dfs(self, root):
         if not root:
             return 0
         left = self.dfs(root.left)
         right = self.dfs(root.right)
         return left + right + 1

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def getlist(self, root):
         if root == None:
             return []
         
         s = []
         
         if root.left != None:
             s = s + self.getlist(root.left)
         s = s + [root.val]
         if root.right != None:
             s = s + self.getlist(root.right)
         return s
             
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         s = self.getlist(root)
         if len(s) == 0:
             return 0
         return s[k - 1]
         
                 
             

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         stack = []
         curr = root
         count = 0
         while True:
             if curr:
                 stack.append(curr)
                 curr = curr.left
             else:
                 if stack:
                     node = stack.pop()
                     count += 1
                     if count == k: return node.val
                     curr = node.right
                 else:
                     return -1
             
         

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         count=[0]
         ans=[None]
         
         def search (head):
             if not head:
                 return
             if head.left:
                 search(head.left)
             count[0] +=1
             print(k,count[0])
             if count[0]==k:
                 ans[0]=head.val
             search(head.right)
         search(root)
         return ans[0]

# Definition for a binary tree node.
 # class TreeNode:
 #     def __init__(self, x):
 #         self.val = x
 #         self.left = None
 #         self.right = None
 
 class Solution:
     def kthSmallest(self, root, k):
         """
         :type root: TreeNode
         :type k: int
         :rtype: int
         """
         return self.use_in_order_count_iteration(root, k)
     
     def use_binary_search(self, root, k):
         # if tree is a balanced BST
         count = self.count_tree(root.left)
         if count + 1 == k:
             return root.val
         if count + 1 > k:
             return self.use_binary_search(root.left, k)
         else:
             return self.use_binary_search(root.right, k - count - 1)
         
     def count_tree(self, node):
         if not node:
             return 0
         return 1 + self.count_tree(node.left) + self.count_tree(node.right)
     
     def use_in_order_count(self, root, k):
         self.count = k
         self.ret = root.val
         self.in_order_count(root, k)
         return self.ret
     
     def use_in_order_count_iteration(self, root, k):
         
         stack = collections.deque()
         curr = root
         count = k
         ret = root.val
         while stack or curr:
             while curr:
                 stack.append(curr)
                 curr = curr.left
             node = stack.pop()
             count -= 1
             if count == 0:
                 ret = node.val
                 break
             curr = node.right
         return ret
     
     def in_order_count(self, node, k):
         if not node:
             return
         self.in_order_count(node.left, k)
         self.count -= 1
         if self.count == 0:
             self.ret = node.val
             return
         self.in_order_count(node.right, k)
     
     def use_in_order(self, root, k):
         self.data = []
         self.in_order(root)
         return self.data[k-1]
     
     def in_order(self, root):
         if not root:
             return
         self.in_order(root.left)
         self.data.append(root.val)
         self.in_order(root.right)