Runtime error for the code


  • 0
    K
    enter code here
    

    /**

    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • struct TreeNode *left;
      
    • struct TreeNode *right;
      
    • };
      */

    struct TreeNodelca(struct TreeNoderoot,struct TreeNodep,struct TreeNodeq, int *v1, int *v2)
    {
    if(root==NULL) return NULL;
    if(root->val==p->val)
    {
    *v1=1;
    return root;
    }

    if(root->val==q->val) 
    {
            *v2=1;
            return root;
    }
    

    struct TreeNodelefto=lca(root->left,p,q,v1,v2);
    struct TreeNode
    righto=lca(root->right,p,q,v1,v2);
    if(lefto && righto) return root;
    return (lefto!=NULL)?lefto :righto;
    }
    int find(struct TreeNode * root,struct TreeNode * Node)
    {
    if(root==NULL) return 0;
    if(root->val==Node->val||find(root->left,Node)||find(root->right,Node));
    return 1;
    return 0;
    }
    struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    int v1=0,v2=0;
    struct TreeNode*lcas=lca(root,p,q,&v1,&v2);
    // printf("%d",v2);
    if(v1 && v2 || v1 && find(lca,q) || v2 && find(lca,p))
    return lcas;
    return NULL;

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.