Simple Recursive Inorder Traversal - O(n)


#1
int cnt = 0;

TreeNode* solve(TreeNode* root, int K)
{
    if(!root)   return 0;
    TreeNode* temp1 = solve(root->left,K);
    int rank = cnt;
    cnt++;
    if(rank==K) return root;
    TreeNode* temp2 = solve(root->right,K);
    return temp1 ? temp1 : temp2;    
}


int Solution::kthsmallest(TreeNode* A, int B) {
    cnt=1;
    TreeNode* ans=solve(A,B);
    return ans->val;
    
}