```
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> list = new LinkedList<List<Integer>>();
Stack<Integer> temp = new Stack<Integer>();
addPath(root, sum, list, temp);
return list;
}
public void addPath(TreeNode node, int sum, List<List<Integer>> list, Stack temp){
if(node==null) return;
temp.push(node.val);
if(node.val==sum&&node.left==null&&node.right==null) list.add(new LinkedList<Integer>(temp));
else{
addPath(node.left, sum-node.val, list, temp);
addPath(node.right, sum-node.val, list, temp);
}
temp.pop();
}
```