Extremely easy solution O(n) recursive soln


#1

int solve(TreeNode A,int B,int C,int& ans){
if(A== NULL){
return 0;
}
int left = solve(A->left,B,C,ans);
int right = solve(A->right,B,C,ans);
int sum = left+right;
if(A->val == B){
sum=sum+1;
}
if(A->val == C){
sum=sum+1;
}
if(sum == 2 && ans ==-1){
ans=A->val;
}
return sum;
}
int Solution::lca(TreeNode
A, int B, int C) {
int ans =-1;
solve(A,B,C,ans);
return ans;
}