java: add «437. Path Sum III»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-19 20:47:24 +02:00
parent 5632842740
commit 9f9d400b2d
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

21
java/path-sum-iii.java Normal file
View file

@ -0,0 +1,21 @@
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);
}
}