package algorithm;

/**
 * Created by yefeng on 16/11/19.
 */
public class FindTreePath {
    static public class BTNode {

        public BTNode(int value, BTNode left, BTNode right) {
            this.val_ = value;
            this.left_ = left;
            this.right_ = right;
        }

        int val_;
        BTNode left_ = null;
        BTNode right_ = null;
    }

    public static void printPath(int[] path, int level) {
        for (int i = 0; i < level; ++i) {
            System.out.print(path[i] + " ");
        }
        System.out.println();
    }

    public static void findPath(BTNode root, int sum, int[] path, int level) {
        path[level++] = root.val_;
        sum -= root.val_;
        if (root.left_ == null && root.right_ == null) {
            if (sum == 0) {
                printPath(path, level);
            }
        } else if (sum > 0){
            if (root.left_ != null) findPath(root.left_, sum, path, level);
            if (root.right_ != null) findPath(root.right_, sum, path, level);
        }
    }


    public static void main(String[] args) {
        BTNode n1 = new BTNode(4, null, null);
        BTNode n2 = new BTNode(7, null, null);
        BTNode n3 = new BTNode(5, n1, n2);
        BTNode n4 = new BTNode(12, null, null);
        BTNode n5 = new BTNode(10, n3, n4);

        int[] path = new int[10];
        findPath(n5, 19, path, 0);
    }
}

@@ -51,16 +51,178 @@
 /11/19.%0A
+ * %E9%A2%98%E7%9B%AE:%E8%BE%93%E5%85%A5%E4%B8%80%E4%B8%AA%E6%95%B4%E6%95%B0%E5%92%8C%E4%B8%80%E6%A3%B5%E4%BA%8C%E5%85%83%E6%A0%91%E3%80%82 %E4%BB%8E%E6%A0%91%E7%9A%84%E6%A0%B9%E7%BB%93%E7%82%B9%E5%BC%80%E5%A7%8B%E5%BE%80%E4%B8%8B%E8%AE%BF%E9%97%AE%E4%B8%80%E7%9B%B4%E5%88%B0%E5%8F%B6%E7%BB%93%E7%82%B9%E6%89%80%E7%BB%8F%E8%BF%87%E7%9A%84%E6%89%80%E6%9C%89%E7%BB%93%E7%82%B9%E5%BD%A2%E6%88%90%E4%B8%80%E6%9D%A1%E8%B7%AF%E5%BE%84%E3%80%82 %E6%89%93%E5%8D%B0%E5%87%BA%E5%92%8C%E4%B8%8E%E8%BE%93%E5%85%A5%E6%95%B4%E6%95%B0%E7%9B%B8%E7%AD%89%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84%E3%80%82%0A * %E4%BE%8B%E5%A6%82%E8%BE%93%E5%85%A5%E6%95%B4%E6%95%B022%E5%92%8C%E5%A6%82%E4%B8%8B%E4%BA%8C%E5%85%83%E6%A0%91%0A *    10%0A *    /%5C%0A *   5 12%0A *  /%5C%0A * 4 7%0A * %E5%88%99%E6%89%93%E5%8D%B0%E5%87%BA%E4%B8%A4%E6%9D%A1%E8%B7%AF%E5%BE%84:10, 12 %E5%92%8C10, 5, 7%E3%80%82%0A
  */%0Apubl

add files