LCA Problem Implementation

I have implemented the question on IinterviewBit. Runtime Error is occuring. PLease help me to find the mistake

Ques Link (InterviewBit) :- https://www.interviewbit.com/problems/least-common-ancestor/

my code :-

/**

  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    */

class custom{
public:
TreeNode* LCAnode;
bool p;
bool q;
};

custom* calcLCA(TreeNode* root,int p,int q){
if(root==NULL){
return NULL;
}

custom* ls=calcLCA(root->left,p,q);
if(ls!=NULL && ls->LCAnode!=NULL){
    return ls;
}

custom* rs=calcLCA(root->right,p,q);
if(rs!=NULL && rs->LCAnode!=NULL){
    return rs;
}

custom* result=new custom();

if(ls!=NULL && rs!=NULL){
    result->LCAnode=root;
    result->p=true;
    result->q=true;
    return result;
}

else if(root->val == p){
    result->p=true;
    result->q = (ls)?ls->q:false or (rs)?rs->q:false;
    
    if(result->p==true && result->q==true){
        result->LCAnode=root;
    }
    return result;
}

else if(root->val == q){
    result->q=true;
    result->p = (ls)?ls->p:false or (rs)?rs->p:false;
    
    if(result->p==true && result->q==true){
        result->LCAnode=root;
    }
    return result;
}

else if(ls!=NULL){
    return ls;
}

else if(rs!=NULL){
    return rs;
}

return NULL;

}

int Solution::lca(TreeNode* A, int B, int C) {
custom* ans=calcLCA(A,B,C);
if(ans==NULL){
return -1;
}
return ans->LCAnode->val;
}

my code’s link is :- https://ide.codingblocks.com/s/232048

@krikhi
The input for which your code failed as you know has only 1 root node
And p and q are also 1
So you have to check for condition if root has value equal to both p and q