Simpler and Easiest solution


#1

Break into two part -

  1. Check if both the values are present. If not present return -1;
  2. If Present then go with popular solution.

public TreeNode findLCA(TreeNode root, int n1, int n2)
{
if(root==null) return null;

    if(root.val==n1 || root.val==n2)
    return root; 
    
    TreeNode leftlca=findLCA(root.left, n1, n2);
    TreeNode rightlca=findLCA(root.right, n1, n2);
    
    if(leftlca!=null && rightlca!=null)
       return root; 
    
     if(leftlca!=null)
    return leftlca;
    
    return rightlca;
    
    
   
}

public boolean findNode(TreeNode root, int n1)
{
    if(root==null)
    return false;
    
    if(root.val==n1)
       return true;
     
    boolean rootleft=findNode(root.left,n1);
    boolean rootright=findNode(root.right, n1);
    
    return (rootleft || rootright);
       
}