<p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is <code>n</code>.</li>
	<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>

# 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: Optional[TreeNode], k: int) -> int:
        
#         stack = []
#         cnt = 0
#         node = root
#         while node:
#             stack.append(node)
#             node = node.left
        
#         while stack:
#             node = stack.pop()
#             cnt += 1
#             if cnt == k:
#                 return node.val
#             node = node.right
#             while node:
#                 stack.append(node)
#                 node = node.left
            
                
        cnt = 1
        node = root
        while node:
            if node.left:
                pnode = node.left
                # find connect
                while pnode.right:
                    pnode = pnode.right
                
                # link
                pnode.right = node
                # node goes to 
                node = node.left
                pnode.right.left = None
            else:
                if cnt == k:
                    return node.val
                cnt += 1
                node = node.right
                
        # cnt = 1
#         while root:
#             if root.left:
#                 node = root.left
#                 # find left tree's right most
#                 while node.right:
#                     node = node.right
#                 # left most connect to current node 
#                 node.right = root
#                 # goes to left node
#                 root = root.left
#                 # set prev current node's left to be None
#                 node.right.left = None
#             else:
#                 if cnt == k:
#                     return root.val
#                 cnt += 1
#                 root = root.right
#         while root:
#             if root.left:
#                 node = root.left
#                 while node.right:
#                     node = node.right
                    
#                 node.right = root
#                 root = root.left
#                 node.right.left = None  
#             else:
#                 if cnt == k:
#                     return root.val
#                 cnt += 1
#                 root = root.right
 
                    
        
18300