Skip to content

Commit d4c06fc

Browse files
refactor 112
1 parent fb801aa commit d4c06fc

File tree

1 file changed

+10
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+10
-27
lines changed

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,16 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5-
/**
6-
* 112. Path Sum
7-
8-
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
9-
10-
For example:
11-
Given the below binary tree and sum = 22,
12-
13-
5
14-
/ \
15-
4 8
16-
/ / \
17-
11 13 4
18-
/ \ \
19-
7 2 1
20-
21-
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.*/
225
public class _112 {
23-
public static class Solution1 {
24-
public boolean hasPathSum(TreeNode root, int sum) {
25-
if (root == null) {
26-
return false;
27-
}
28-
if (root.val == sum && root.left == null && root.right == null) {
29-
return true;
30-
}
31-
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
6+
public static class Solution1 {
7+
public boolean hasPathSum(TreeNode root, int sum) {
8+
if (root == null) {
9+
return false;
10+
}
11+
if (root.val == sum && root.left == null && root.right == null) {
12+
return true;
13+
}
14+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
15+
}
3216
}
33-
}
3417
}

0 commit comments

Comments
 (0)