Skip to content

Commit 51ce8af

Browse files
refactor 437
1 parent 91bb8ce commit 51ce8af

File tree

1 file changed

+13
-9
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-9
lines changed

src/main/java/com/fishercoder/solutions/_437.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fishercoder.common.classes.TreeNode;
44

55
/**
6+
* 437. Path Sum III
7+
*
68
* You are given a binary tree in which each node contains an integer value.
79
810
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
3234

3335
public class _437 {
3436

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);
3843
}
39-
return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
40-
}
4144

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);
4550
}
46-
return (root.val == sum ? 1 : 0) + pathSumFrom(root.left, sum - root.val) + pathSumFrom(root.right, sum - root.val);
4751
}
4852

4953
}

0 commit comments

Comments
 (0)