private static List<BinaryNode> getPathTo(BinaryNode root, BinaryNode target) {

    List<BinaryNode> path = new LinkedList<BinaryNode>();
    if (root == target) {
      path.add(target);
    } else {
      if (root.getLeft() == null && root.getRight() == null) {
        return null;
      }
      if (root.getLeft() != null) {
        List<BinaryNode> pathTo = getPathTo(root.getLeft(), target);
        if (pathTo != null) {
          path.add(root);
          path.addAll(getPathTo(root.getLeft(), target));
        }
      }
      if (root.getRight() != null) {
        List<BinaryNode> pathTo = getPathTo(root.getRight(), target);
        if (pathTo != null) {
          path.add(root);
          path.addAll(pathTo);
        }
      }
    }
    return path;
  }
Iterative solution for this problem. The complexity order of this algorithm is the same in
memory terms, O(N), but is worst in space terms because we are using a list of BinaryNodes
that is going to suppose O(N) for space terms where N is the path to the element.

This algorithm is based on find the path of two nodes given a root node and compare both paths
to find the first non common element and return the previous node, the lowest common ancestor.
src/main/java/com/github/pedrovgs/problem21/LowestCommonAncestor.java