import java.util.ArrayList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
ArrayList<Integer> list= new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int expectNumber) {
if(root==null){
return res;
}
dfs(root,expectNumber);
return res;
}
private void dfs(TreeNode root,int target){
list.add(root.val);
if(root.val==target&&root.left==null&&root.right==null){
res.add(new ArrayList(list));
}
if(root.left!=null){
dfs(root.left,target-root.val);
}
if(root.right!=null){
dfs(root.right,target-root.val);
}
list.remove(list.size()-1);
return;
}
}
思路:深度优先遍历加回溯