C ++ using recursion


#1
void final(TreeNode* A, int a,vector<int> &res,vector<vector<int>> &temp)
{   
    if(A==NULL)
        return;
    res.push_back(A->val);
    if(A->val==a)
    {
        temp.push_back(res);
        return;
    }
    final(A->left,a,res,temp);
    final(A->right,a,res,temp);
    res.pop_back();
    
    
}
int Solution::lca(TreeNode* A, int B, int C) {
    vector<int> res1,res;
    vector<vector<int>> v;
    
    final(A,B,res,v);
    final(A,C,res1,v);
    if(v.size()<2)
        return -1;
    int i,j;
    for(i=0,j=0;i<v[0].size(),j<v[1].size();i++,j++)
    {
        if(v[0][i]!=v[1][j])
        {
            break;
        }
    }
    return v[0][i-1];
}