-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path Sum.java
32 lines (25 loc) · 1.02 KB
/
Path Sum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static int sumHelper(TreeNode root,int sum){
if(root==null){
return 0;
}
int leftval = sumHelper(root.left,sum-root.val);
if(leftval==Integer.MAX_VALUE) return Integer.MAX_VALUE;
int rightval = sumHelper(root.right,sum-root.val);
if(rightval==Integer.MAX_VALUE) return Integer.MAX_VALUE;
if(leftval==0&&rightval==0&&root.val==sum){
return Integer.MAX_VALUE;
}
return Integer.MIN_VALUE;
}
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null) return false;
int treesum = sumHelper(root,sum);
return treesum==Integer.MAX_VALUE?true:false;
}
//better solution
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;
sum -= root.val;
if(root.left == null && root.right==null) return sum == 0;
else return hasPathSum(root.left,sum) || hasPathSum(root.right,sum);
}