File tree Expand file tree Collapse file tree 1 file changed +15
-10
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +15
-10
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import com .fishercoder .common .classes .TreeNode ;
4
4
5
- /**112. Path Sum
5
+ /**
6
+ * 112. Path Sum
7
+
6
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.
7
9
8
10
For example:
9
11
Given the below binary tree and sum = 22,
12
+
10
13
5
11
14
/ \
12
15
4 8
13
16
/ / \
14
17
11 13 4
15
18
/ \ \
16
19
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.*/
18
22
public class _112 {
23
+ public static class Solution1 {
19
24
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 );
27
32
}
28
-
33
+ }
29
34
}
You can’t perform that action at this time.
0 commit comments