437. 路径总和 III
mport java.util.ArrayList;
import java.util.List;
public class Problem_437_PathSum {
public int pathSum(TreeNode root, int target) {
return dfs(root, new ArrayList<>(), target);
}
public int dfs(TreeNode node, List<Long> parentPathSumList, int targetSum) {
if (node == null) return 0;
int cnt = 0;
List<Long> tmp = new ArrayList<>();
for (int i = 0; i < parentPathSumList.size(); i++) {
long sum = parentPathSumList.get(i) + node.val;
tmp.add(sum);
if (sum == targetSum) cnt++;
}
tmp.add((long) node.val);
if (node.val == targetSum) cnt++;
int leftCnt = dfs(node.left, tmp, targetSum);
int rightCnt = dfs(node.right, tmp, targetSum);
return cnt + leftCnt + rightCnt;
}
}