public void hasPathSum(TreeNode root, int sum, ArrayList<ArrayList<Integer>> result, ArrayList<TreeNode> path) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (root == null) {
            return;
        } else if (root.left == null && root.right == null) {
            if (sum != root.val) {
                return;
            }
            ArrayList<Integer> p = new ArrayList<Integer>();
            for (TreeNode t : path) {
                p.add(t.val);
            }
            p.add(root.val);
            result.add(p);
        } else {
            path.add(root);
            sum -= root.val;
            hasPathSum(root.left, sum, result, path);
            hasPathSum(root.right, sum, result, path);
            path.remove(path.size() - 1);
        }
    }