Easy to understand soln cpp


#1

bool tree(TreeNode* root,int x){
if(root==NULL)
return false;
if(root->val==x)
return true;
bool l = tree(root->left,x);
if(l)
return l;
bool r = tree(root->right,x);
if®
return r;

 return false;

}
int slca(TreeNode*A,int B,int C){
if(A==NULL)
return -1;
if(A->val==B||A->val==C)
return A->val;

int l = slca(A->left,B,C);
int r = slca(A->right,B,C);
if(l!=-1&&r!=-1)
return A->val;
if(l&&r==-1)
    return l;
if(r&&l==-1)
    return r;
return -1;

}
int Solution::lca(TreeNode* A, int B, int C) {
if(tree(A,B)==false)
return -1;
if(tree(A,C)==false)
return -1;
return slca(A,B,C);
}