Ŀ

һŶһӡнֵĺΪ··Ϊĸ㿪ʼһֱҶĽγһ·

##

һŶһ

##

нֵĺΪ·

##Ŀ

ڵ

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

    public TreeNode(int val) {
        this.val = val;

    }

}

ⷨ ʱ䣺31msռڴ棺652k

public class Solution {
    //ڴ洢·
    ArrayList<ArrayList<Integer>> paths=new ArrayList<ArrayList<Integer>>();
    
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {        
        if(root==null) return paths;
        //ݹ
        SearchPath(new ArrayList<Integer>(),root,target);
        return paths;
    }
    public void SearchPath(ArrayList<Integer> path,TreeNode root,int target){
        if(root==null) return;
        //ѵǰڵ·ͬʱtargetҪȥǰڵֵ
        //pathtargetֵݵľֲ
        path.add(root.val);
        target=target-root.val;
        
        if(root.left==null&&root.right==null){
            //targetΪҶӽڵӦ··뵽·
            if(target==0){
             paths.add(path);
            } 
            return;
        }
        //Ҷӽڵ±һ·
        ArrayList<Integer> path2=new ArrayList<>();
        path2.addAll(path);
        //ݹҽڵ
        SearchPath(path,root.left,target);
        SearchPath(path2,root.right,target);
    }
}

ݹÿһڵʱѽڵ·targetȥǰڵֵΪҶӽڵ㣬жtargetǷΪ㣬Ϊһɹ·Ҷӽڵͼӽڵ