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<>>
     */
    public ArrayList<ArrayList<Integer>> pathSum (TreeNode root, int sum) {
        // write code here
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if(root == null){
            return result; 
        }
        find(root,sum,new ArrayList<Integer>(),result);
        return result;
    }

    private void find(TreeNode root,int curValue,ArrayList<Integer> curList,ArrayList<ArrayList<Integer>> result){
        ArrayList<Integer> subList = new ArrayList<>(curList);
        subList.add(root.val);
        if(root.left == null && root.right == null){//叶子节点
            if(curValue == root.val){
                result.add(subList);
            }
        } else {
            //继续顺着树往下寻找
            if(root.left != null){
                find(root.left,curValue - root.val,subList,result);
            } 
            if(root.right != null) {
                find(root.right,curValue - root.val,subList,result);
            }
        }
    }
}