java: add «437. Path Sum III»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
5632842740
commit
9f9d400b2d
1 changed files with 21 additions and 0 deletions
21
java/path-sum-iii.java
Normal file
21
java/path-sum-iii.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue