/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem38;

/**
 * Given a binary tree, can you write a method to calculate every path to every leaf in the tree.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class PathToEveryLeaf {
}

@@ -645,16 +645,119 @@
 lem38;%0A%0A
+import com.github.pedrovgs.binarytree.BinaryNode;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0A%0A
 /**%0A * G
@@ -927,11 +927,1076 @@
 ryLeaf %7B
+%0A%0A  /**%0A   * Recursive algorithm to solve this problem. The complexity order of this algorithm is O(N)%0A   * where N is the number of elements in the array in time and space terms.%0A   */%0A  public List%3CList%3CBinaryNode%3E%3E calculate(BinaryNode%3CInteger%3E root) %7B%0A    if (root == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass a null instance of BinaryNode%22);%0A    %7D%0A    return calculatePathToLeafs(root, new LinkedList%3CBinaryNode%3E());%0A  %7D%0A%0A  private static List%3CList%3CBinaryNode%3E%3E calculatePathToLeafs(BinaryNode%3CInteger%3E root,%0A      List%3CBinaryNode%3E path) %7B%0A    List%3CList%3CBinaryNode%3E%3E paths = new LinkedList%3CList%3CBinaryNode%3E%3E();%0A    if (root == null) %7B%0A      return paths;%0A    %7D else if (root.getLeft() == null && root.getRight() == null) %7B%0A      path.add(root);%0A      paths.add(path);%0A      return paths;%0A    %7D else %7B%0A      path.add(root);%0A      paths.addAll(calculatePathToLeafs(root.getLeft(), new LinkedList%3CBinaryNode%3E(path)));%0A      paths.addAll(calculatePathToLeafs(root.getRight(), new LinkedList%3CBinaryNode%3E(path)));%0A      return paths;%0A    %7D%0A  %7D
 %0A%7D%0A

Add recursive solution to problem 38