public static Node findNode(Node root, int n){
if(root != null){
if( root.data == n)
return root;
Node l = findNode(root.left, n);
Node r = findNode(root.right, n);
if(l != null)
return l;
else if(r != null)
return r;
}
return null;
}
Node LCA(Node r, int n1, int n2){ if(r != null){ if(r.data == n1 || r.data == n2){ return r; Node left = LCA(r.left, n1, n2); Node right = LCA(r.right, n1, n2); if(left != null) return left; if(right != null) return right; } }
public static Node LCA_BasedOnFindNode(Node r, int n1, int n2){
if(r != null){
if(r.data == n1 || r.data == n2)
return r;
Node left = LCA_BasedOnFindNode(r.left, n1, n2);
Node right = LCA_BasedOnFindNode(r.right, n1, n2);
if(left != null && right != null)
return r;
else if(left != null)
return left;
else if(right != null)
return right;
}
return null;
}