LCA Of Binary Tree

PHOTO EMBED

Mon Dec 18 2023 05:27:43 GMT+0000 (Coordinated Universal Time)

Saved by @nistha_jnn #c++

int lowestCommonAncestor(TreeNode<int> *root, 
int x, int y)
{
	if(!root)
    return -1;
    if(root->data==x or root->data==y)
    return root->data;
    int lefti=lowestCommonAncestor(root->left,x,y);
    int righti=lowestCommonAncestor(root->right,x,y);
    if(lefti==-1)
    return righti;
    if(righti==-1)
    return lefti;
    return root->data;
}
content_copyCOPY