Java
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>> ret = new ArrayList<>();
ArrayList<Integer> list = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
if (root != null){
list.add(root.val);
target = target - root.val;
//递归到叶节点
if (root.left == null && root.right == null && target == 0)
ret.add(new ArrayList<>(list));
FindPath(root.left, target); //递归左子树
FindPath(root.right, target); //递归右子树
list.remove(list.size() - 1); //如果叶节点不等target,就删除这个节点
}
return ret;
}
}