Can't find what is wrong


#1

Can anyone tell me why I am getting errors? I could not find any wrong here.
TreeNode* Solution::getSuccessor(TreeNode* A, int B) {
TreeNode* curr= A;
while(curr != 0)
{
if(curr -> val == B) break;
else if(curr -> val > B) curr = curr -> left;
else curr = curr -> right;
}
if(curr == NULL) return NULL;
TreeNode* temp = curr;
if(curr -> right != 0)
{
while(temp -> left != 0) {temp = temp -> left;}
return temp;
}
// curr = temp;
temp = curr;
TreeNode *ances=A;
TreeNode *suc=0;
while(ances != curr)
{
if(ances -> val > B)
{
suc = ances;
ances = ances -> left;
}
else ances = ances -> right;
}
return suc;
}


#2

Hey, I found the problem and after fixing it. It is working.

In the below code, while checking the right subtree for next greater element. You got to step up as right node. You missed " temp = temp->right; "

if(curr -> right != 0 ){

    temp = temp->right;
    
    while(temp -> left != 0) {temp = temp -> left;}
    
    return temp;
}