Unable to find where I am wrong?


#1

This is my code

TreeNode* Solution::getSuccessor(TreeNode* A, int B)
{
TreeNode* s=A;
TreeNode* p=nullptr;
TreeNode* tp;
tp=A;
if(tp->left==nullptr&&tp->right==nullptr) return nullptr;
while(tp->val!=B)
{
p=tp;
if(B>(p->val))tp=p->right;
else tp=p->left;

}
if((s->val)>=(tp->val))
{
     if(tp->right==nullptr)return p;
     else
     {
       p=tp;
       tp=p->right;
       while(tp!=nullptr)
       {
           p=tp;
           tp=p->left;
       }
       return p;
     }
}
else  return (tp->right); 

}