Whats wrong in this? Please help


#1
TreeNode* search(TreeNode* root,int val){
    if(root==NULL)return NULL;
    if(root->val>val)root=root->left;
    else if(root->val<val)root=root->right;
    else return root;
} 

TreeNode* Solution::getSuccessor(TreeNode* A, int B) {
    TreeNode* curr=search(A,B);
    if(curr==NULL)return NULL;
    
    //Case 1:
    
    if(curr->right!=NULL){
        TreeNode* temp=curr->right;
        while(temp->left!=NULL){
            temp=temp->left;
        }
        return temp;
    }
    // Case 2:
    
    else{
        TreeNode* ancestor=A;
        TreeNode* successor=NULL;
        while(ancestor!=curr){
            if(curr->val<ancestor->val){
                successor=ancestor;
                ancestor=ancestor->left;
            }
            else ancestor=ancestor->right;
        }
        return successor;
    }
}

#2

Everything is wrong my boi!


#3

If you think so then I suggest go and do some work.


#5

Modify your search function to:

    TreeNode* search(TreeNode* root, int val){
         if(root==NULL) return NULL;
         if(val==root->val) return root;
         else if(val  < root->val) return search(root->left, val);
         else return search(root->right, val);
    }

In your while loop, you need to compare ancestor->val with B not with curr->val

      while(ancestor!=curr){
            if(B < ancestor->val){
                successor = ancestor->val;
                ancestor = ancestor->left;
            }
            else 
                ancestor = ancestor->right;
    }