1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 18:07:32 +01:00
LeetCode/java/path-sum-iii.java

22 lines
519 B
Java
Raw Normal View History

class Solution {
private int pathSumIncluding(TreeNode node, long target) {
if (node == null) {
return 0;
}
return (node.val == target ? 1 : 0)
+ pathSumIncluding(node.left, target - node.val)
+ pathSumIncluding(node.right, target - node.val);
}
public int pathSum(TreeNode node, int targetSum) {
if (node == null) {
return 0;
}
return pathSumIncluding(node, targetSum)
+ pathSum(node.left, targetSum)
+ pathSum(node.right, targetSum);
}
}