private List<List<Integer>> lists = new ArrayList<List<Integer>>();
private List<Integer> path = new ArrayList<Integer>();
private int sum;
public List<List<Integer>> binaryTreePathSum2(TreeNode root, int target) {
sum = 0;
helper(root, target);
return lists;
}
private void helper(TreeNode root, int target) {
// Write your code here`
if (root == null) {
return;
}
sum += root.val;
path.add(root.val);
if (root.left == null && root.right == null && sum == target) {
// compare sum & target
// the thing of add is the reference of path.
List<Integer> l = new ArrayList<Integer>();
for (int i = 0; i < path.size(); i++) {
l.add(path.get(i));
}
lists.add(l);
}
helper(root.left, target);
helper(root.right, target);
sum -= path.get(path.size() - 1);
path.remove(path.size() - 1);
}