@@ -0,0 +1,1965 @@
+package com.sunny.grokkingalgorithms.ctci.c4;%0A%0Aimport com.sunny.grokkingalgorithms.ctci.c4.util.BinaryTreeNode;%0Aimport com.sunny.grokkingalgorithms.ctci.c4.util.TreeUtils;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * Created by sundas on 3/21/2018.%0A */%0Apublic class PrintAllPathsThatAddToAValue %7B%0A%0A  /*%0A  Print all possible paths that add to value%0A   */%0A%0A  /**%0A   *%0A   * @param root%0A   * @param k%0A   */%0A   public static void printPossiblePaths(BinaryTreeNode root,int k)%7B%0A     int depth = depth(root);%0A     int%5B%5D paths = new int%5Bdepth%5D;%0A     printPossiblePaths(root,k,paths,0);%0A   %7D%0A%0A   public static void printPossiblePaths(BinaryTreeNode root,int k,int%5B%5D paths,int level)%7B%0A     if(root == null)%7B%0A       return;%0A     %7D%0A     paths%5Blevel%5D = root.data;%0A     /*%0A     Does this node add up to the sum%0A      */%0A     int runningSum = 0;%0A     for(int i = level ; i %3E= 0 ; i--)%7B%0A       runningSum += paths%5Bi%5D;%0A       if(runningSum == k)%7B%0A         printPath(paths,i,level);%0A       %7D%0A     %7D%0A     printPossiblePaths(root.left,k,paths,level+1);%0A     printPossiblePaths(root.right,k,paths,level+1);%0A     paths%5Blevel%5D = Integer.MIN_VALUE;%0A   %7D%0A%0A  /**%0A   *%0A   * @param paths%0A   * @param start%0A   * @param end%0A   */%0A  public static void printPath(int%5B%5D paths,int start,int end)%7B%0A    for(int i = start ; i %3C= end ; i++)%7B%0A      System.out.print(paths%5Bi%5D + %22-%3E%22);%0A    %7D%0A    System.out.println();%0A  %7D%0A%0A  /**%0A   *%0A   *%0A   * @param root%0A   * @return%0A   */%0A   public static int depth(BinaryTreeNode root)%7B%0A     if(root == null)%7B%0A       return 0;%0A     %7D%0A     return 1 + Math.max(depth(root.left),depth(root.right));%0A   %7D%0A%0A%0A  /**%0A   *%0A   * @param args%0A   */%0A  public static void main(String%5B%5D args) %7B%0A    BinaryTreeNode root = TreeUtils.createBalancedBinaryTree();%0A    TreeUtils.performBfsLevelMarker(root);%0A    printPossiblePaths(root, 8);%0A    printPossiblePaths(root,9);%0A    printPossiblePaths(root,7);%0A    printPossiblePaths(root,4);%0A    printPossiblePaths(root,6);%0A  %7D%0A%0A%7D

Print all paths that add up to a number k.