Skip to content

Commit 4cebc5c

Browse files
refactor 112
1 parent 6b7d467 commit 4cebc5c

File tree

1 file changed

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

1 file changed

+15
-10
lines changed

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

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

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

5-
/**112. Path Sum
5+
/**
6+
* 112. Path Sum
7+
68
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.
79
810
For example:
911
Given the below binary tree and sum = 22,
12+
1013
5
1114
/ \
1215
4 8
1316
/ / \
1417
11 13 4
1518
/ \ \
1619
7 2 1
17-
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.*/
20+
21+
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.*/
1822
public class _112 {
23+
public static class Solution1 {
1924
public boolean hasPathSum(TreeNode root, int sum) {
20-
if (root == null) {
21-
return false;
22-
}
23-
if (root.val == sum && root.left == null && root.right == null) {
24-
return true;
25-
}
26-
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
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);
2732
}
28-
33+
}
2934
}

0 commit comments

Comments
 (0)