My solution shows memory limit exceeded


#1

Can someone help me find out whats wrong with my solution? It shows memory limit exceeded.

   TreeNode* LCA(TreeNode* rt,int a,int b){
        if(rt==NULL)
        return NULL;
        if(rt->val==a || rt->val==b)
        return rt;
        TreeNode* leftLCA=LCA(rt->left,a,b);
        TreeNode* rightLCA=LCA(rt->right,a,b);
        if(leftLCA && rightLCA) return rt;
        if(leftLCA==NULL) return rightLCA;
        return leftLCA;
    }
    bool search(TreeNode* rt,int t){
        if(rt==NULL)
        return false;
        if(rt->val==t)
        return true;
        return search(rt->left,t) || search(rt->right,t);
        
    }
    int Solution::lca(TreeNode* A, int B, int C) {
        if(!search(A,B) || !search(A,C))
        return -1;
        TreeNode* ans= LCA(A,B,C);
        
        if(ans)
        return ans->val;
        return -1;
    }