File tree 1 file changed +13
-9
lines changed
src/main/java/com/fishercoder/solutions 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change 3
3
import com .fishercoder .common .classes .TreeNode ;
4
4
5
5
/**
6
+ * 437. Path Sum III
7
+ *
6
8
* You are given a binary tree in which each node contains an integer value.
7
9
8
10
Find the number of paths that sum to a given value.
@@ -32,18 +34,20 @@ The path does not need to start or end at the root or a leaf, but it must go dow
32
34
33
35
public class _437 {
34
36
35
- public int pathSum (TreeNode root , int sum ) {
36
- if (root == null ) {
37
- return 0 ;
37
+ public static class Solution1 {
38
+ public int pathSum (TreeNode root , int sum ) {
39
+ if (root == null ) {
40
+ return 0 ;
41
+ }
42
+ return pathSumFrom (root , sum ) + pathSum (root .left , sum ) + pathSum (root .right , sum );
38
43
}
39
- return pathSumFrom (root , sum ) + pathSum (root .left , sum ) + pathSum (root .right , sum );
40
- }
41
44
42
- private int pathSumFrom (TreeNode root , int sum ) {
43
- if (root == null ) {
44
- return 0 ;
45
+ private int pathSumFrom (TreeNode root , int sum ) {
46
+ if (root == null ) {
47
+ return 0 ;
48
+ }
49
+ return (root .val == sum ? 1 : 0 ) + pathSumFrom (root .left , sum - root .val ) + pathSumFrom (root .right , sum - root .val );
45
50
}
46
- return (root .val == sum ? 1 : 0 ) + pathSumFrom (root .left , sum - root .val ) + pathSumFrom (root .right , sum - root .val );
47
51
}
48
52
49
53
}
You can’t perform that action at this time.
0 commit comments