Easy backtracking solution!


#1
bool findpath(TreeNode *root,vector<int> &path,int k)
{
    if(!root)
    return false;
    path.push_back(root->val);
    if(root->val==k)
    return true;
    if(findpath(root->left,path,k)||findpath(root->right,path,k))
    return true;
    path.pop_back();
    return false;
}
int Solution::lca(TreeNode* A, int B, int C) {
    vector<int> p1,p2;
    if(!findpath(A,p1,B)||!findpath(A,p2,C))
    return -1;
    int i=0;
    for(i=0;i<p1.size()&&i<p2.size();i++)
    if(p1[i]!=p2[i]) break;
    return p1[i-1];
}