<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:
        # start at the root and process the nodes in order, counting
        # the number of nodes processed and returning once we have
        # processed the kth node
        curr = root
        count = 0

        # perform a Morris Inorder Traversal
        # where we restructure the BST to be a completely
        # right-skewed tree
        while curr:
            # if the current node has a left node, then we traverse
            # because we want to perform an inorder traversal.
            if curr.left:
                # the left child's right most node is going to be the
                # parent of the current node in the final right skewed tree
                # this is because it is the largest value of the left child of
                # the current node which is the largest value smaller than the
                # current node but larger than the left child
                temp = curr.left
                while temp and temp.right:
                    temp = temp.right
                
                # we make right most child's right child point to the current node
                temp.right = curr

                # since the right most child of the left child is now pointing to the
                # left's parent, we created a cycle. To eliminate this cycle, we remove
                # the left pointer from the parent and continue traversing the tree.
                left, curr.left = curr.left, None
                curr = left
            else:
                # if at any point we don't have a left child anymore,
                # we know this is the smallest node in the tree, 1. 
                # because the left-most node in a BST is the smallest, 
                # 2. because the tree is now a right-skewed tree, meaning
                # the node is in order of right children
                count += 1
                if count == k:
                    return curr.val
                curr = curr.right
18400