You are given a binary tree with the value of two nodes in it. Write an algorithm to find the lowest common ancestor.
public static Node findLCA(Node root, Node p, Node q){ /* If there is no root */ if(!root) return null; /* if either p or q is the root then root is LCA */ if(root == p || root == q) return root; else{ /* get LCA of p and q in left subtree */ Node left = findLCA(root.left, p, q); /* get LCA of p and q in right subtree */ Node right = findLCA(root.right, p, q); /* if any of p or q is in left subtree and other in right : root is LCA */ if(left & & right) return root; /* If left is not null, left is LCA */ elseif(left) return left; else return right; } }