int cnt =0;publicintpathSum(TreeNode root,int target) {if (root ==null) {return cnt; }// do pre-order, // sum is for prefix sum from root to leafArrayList<Integer> sum =newArrayList<>();helper(sum, target, root);return cnt;}privatevoidhelper(ArrayList<Integer> sum,int target,TreeNode root) {// when we reach leaf, we check what we should add to the resif (root ==null) {return; }// add cur node value to sumsum.add(0);for (int i =0; i <sum.size(); i++) {int newSum =sum.get(i) +root.val;sum.set(i, newSum);// also check if we add up to target yet, if so, add to resif (newSum == target) { cnt++; } }if (root.left!=null) {helper(sum, target,root.left);sum.remove(sum.size() -1);for (int i =0; i <sum.size(); i++) {int newSum =sum.get(i) -root.left.val;sum.set(i, newSum); } }if (root.right!=null) { helper(sum, target,root.right);sum.remove(sum.size() -1);for (int i =0; i <sum.size(); i++) {int newSum =sum.get(i) -root.right.val;sum.set(i, newSum); } }}