Skip to content

Commit 5cc4816

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 437_Path_Sum_III.java
1 parent 7c6809c commit 5cc4816

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int pathSum(TreeNode root, int sum) {
3+
if (root == null) {
4+
return 0;
5+
}
6+
7+
return traverseSubtree(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
8+
}
9+
10+
private int traverseSubtree(TreeNode root, int sum) {
11+
int result = 0;
12+
if (root == null) {
13+
return result;
14+
}
15+
16+
if (root.val == sum) {
17+
++result;
18+
}
19+
20+
return result + traverseSubtree(root.left, sum - root.val) + traverseSubtree(root.right, sum - root.val);
21+
}
22+
}

0 commit comments

Comments
 (0)