Lowest Common Ancestor in a Binary Tree

PHOTO EMBED

Sun Oct 09 2022 18:39:05 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution
{
    public:
    //Function to return the lowest common ancestor in a Binary Tree.
    Node* lca(Node* root,int n1,int n2 )
    {
        if(root==NULL) return NULL;
        if(root->data==n1 || root->data==n2) return root;
        
        Node* left=lca(root->left,n1,n2);
        Node* right=lca(root->right,n1,n2);
         
         if(left==NULL) return right;
         if(right==NULL) return left;
         return root;
        
       //Your code here 
    }
};
content_copyCOPY

https://practice.geeksforgeeks.org/problems/lowest-common-ancestor-in-a-binary-tree/1?page=1&company[]=PayPal&sortBy=submissions