https://leetcode.com/problems/kth-smallest-element-in-a-bst/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    int c = 0;
    int ans = 0;
    int kthSmallest(TreeNode* root, int k) {
        if (root == NULL) return ans;
        kthSmallest(root -> left, k);
        c += 1;
        if (c == k) ans = root -> val;
        kthSmallest(root -> right, k);
        return ans;
    }
};

Time Complexity: O(n)
Space: O(1)