Find the Least common ancestor for two given nodes in a tree. I solved this question using double recursion but the interviewer was expecting me to solve it in a better way.
Sigiloso
//i and j are the values of node, whose common ancestor is to be searched. // too2 is the root of the tree private static void coommonancestor(Node root2, int i, int j) { if(i == j && j == root2.data) System.out.println("Both nodes are same"); if(root2.data i && root2.data > j) coommonancestor(root2.left,i,j); if(root2.data >= i && root2.data <= j) System.out.println("The ancestor of "+i+" and "+j+ " is :" + root2.data); }