<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.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* findpred(TreeNode* curr){
        TreeNode* pred = curr;
        pred = pred -> left;
        while(pred -> right != NULL){
            pred = pred -> right;
        } 
    return pred;
    }
    void solve(TreeNode* &root){
        if(root == NULL || (root -> left == NULL && root -> right == NULL)){
            return;
        }
        TreeNode* curr = root;
        TreeNode* prev = root;
        bool firstleftnull = false;
        TreeNode* rightexist = NULL;
        while(curr != NULL){
            if(curr -> left != NULL){
                TreeNode* pred = findpred(curr);
                if(pred -> right == NULL){
                    pred -> right = curr;
                    curr = curr -> left;
                    prev -> left = NULL;
                    prev = curr;
                }
                else{
                    curr = curr -> left;
                    prev = curr;
                }
            }
            else{
                if(firstleftnull == false){
                    root = curr;
                    firstleftnull = true;
                }
                if(rightexist){
                    rightexist -> right = curr;
                }
                rightexist = curr;
                curr = curr -> right;
                prev = curr;
            }
        }
    }
    int kthSmallest(TreeNode* root, int k) {
        solve(root);
        k = k-1;
        while(k--){
            root = root -> right;
        }
    return root -> val;
    }
};
17500