File tree Expand file tree Collapse file tree 1 file changed +10
-27
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +10
-27
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
- /**
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.*/
22
5
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
+ }
32
16
}
33
- }
34
17
}
You can’t perform that action at this time.
0 commit comments