Simple recursive JAVA implementation


#1
public class Solution {
public ArrayList<ArrayList<Integer>> pathSum(TreeNode A, int B) {
    ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    helper(A, B, new ArrayList<Integer>(), res);
    return res;
    
}
public void helper(TreeNode root, int sum, ArrayList<Integer> curr, 
ArrayList<ArrayList<Integer>> validPaths){
    if(root == null){
        return;
    }
    curr.add(root.val);
    if(root.val == sum && root.left == null && root.right == null){
        validPaths.add(curr);
        return;
    }
    helper(root.left, sum - root.val, new ArrayList<Integer>(curr), validPaths);
    helper(root.right, sum - root.val, new ArrayList<Integer>(curr), validPaths);
}

}