解答
class Solution {
int count = 0;
public int pathSum(TreeNode root, int targetSum) {
pathSumDfs1(root, targetSum);
return count;
}
void pathSumDfs1(TreeNode root, long targetSum) {
if (root == null) {
return;
}
long sum = 0;
pathSumDfs2(root, targetSum, sum);
pathSumDfs1(root.left, targetSum);
pathSumDfs1(root.right, targetSum);
}
void pathSumDfs2(TreeNode root, long targetSum, long sum) {
if (root == null) {
return;
}
sum += root.val;
if (sum == targetSum) {
++count;
}
pathSumDfs2(root.left, targetSum, sum);
pathSumDfs2(root.right, targetSum, sum);
}
}
总结
效率比较感人,虽然通过了所有的用例。
525

被折叠的 条评论
为什么被折叠?



