1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/java/path-sum-iii.java
Matej Focko 9f9d400b2d
java: add «437. Path Sum III»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-08-19 20:47:24 +02:00

21 lines
519 B
Java

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);
}
}