import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @param sum int整型 
     * @return int整型ArrayList<ArrayList<>>
     */
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> pathSum (TreeNode root, int sum) {
        // write code here
        if(root == null) {
            return res;
        }
        ArrayList<Integer> path = new ArrayList<Integer>();
        dfs(root,sum,path);
        return res;

    }

    public void dfs(TreeNode root, int sum, ArrayList<Integer> path) {
        // write code here
        if(root == null) return ;
        sum -= root.val;
        path.add(root.val);


        if(root.left == null && root.right == null) {
            if(sum == 0 ){
                res.add(new ArrayList<Integer>(path));
            }
        }else {
            dfs( root.left,  sum,  path);
            dfs( root.right,  sum,  path);
        }

        path.remove(path.size()-1);


    }
}