@@ -0,0 +1,1772 @@
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A/**%0A * Given a binary tree, return all root-to-leaf paths.%0A *%0A * For example, given the following binary tree:%0A *%0A *    1%0A *  /   %5C%0A * 2     3%0A *  %5C%0A *   5%0A * All root-to-leaf paths are:%0A *%0A * %5B%221-%3E2-%3E5%22, %221-%3E3%22%5D%0A *%0A * Tags: Tree Depth-first Search%0A * Similar Problems: (M) Path Sum II%0A */%0Apublic class BinaryTreePath %7B%0A%0A    /**%0A     * Use helper function to do DFS%0A     */%0A    public List%3CString%3E binaryTreePath1(TreeNode root) %7B%0A        if (root == null) return Collections.emptyList();%0A        List%3CString%3E res = new ArrayList%3C%3E();%0A        dfs(res, root.val + %22%22, root);%0A        return res;%0A    %7D%0A%0A    private void dfs(List%3CString%3E res, String path, TreeNode root) %7B%0A        if (root.left == null && root.right == null) res.add(path);%0A        if (root.left != null) dfs(res, path + %22-%3E%22 + root.left.val, root.left);%0A        if (root.right != null) dfs(res, path + %22-%3E%22 + root.right.val, root.right);%0A    %7D%0A%0A    /**%0A     * No helper%0A     */%0A    public List%3CString%3E binaryTreePath2(TreeNode root) %7B%0A        List%3CString%3E paths = new ArrayList%3C%3E();%0A%0A        if (root == null) %7B%0A            return paths;%0A        %7D%0A%0A        if (root.left == null && root.right == null) %7B%0A            paths.add(root.val + %22%22);%0A            return paths;%0A        %7D%0A%0A        // left subtree%0A        for (String path : binaryTreePath2(root.left)) %7B%0A            paths.add(root.val + %22-%3E%22 + path);%0A        %7D%0A%0A        // right subtree%0A        for (String path : binaryTreePath2(root.right)) %7B%0A            paths.add(root.val + %22-%3E%22 + path);%0A        %7D%0A%0A        return paths;%0A    %7D%0A%0A    public class TreeNode %7B%0A        int val;%0A        TreeNode left;%0A        TreeNode right;%0A        TreeNode(int x) %7B val = x; %7D%0A    %7D%0A%7D%0A

add 257 Binary Tree Paths