I felt this better than ans


#1
TreeNode* Solution::getSuccessor(TreeNode* A, int B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
TreeNode* ansNode=NULL;
TreeNode* crntNode=A;
while(crntNode!=NULL){
    if(B>crntNode->val){
        crntNode=crntNode->right;
    }
    else if(B==crntNode->val){
        crntNode=crntNode->right;
    }
    else{
        if(ansNode==NULL || ansNode->val>crntNode->val){
            ansNode=crntNode;
        }
        crntNode=crntNode->left;
    }
}
return ansNode;

}