-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSum.java
More file actions
38 lines (32 loc) · 857 Bytes
/
PathSum.java
File metadata and controls
38 lines (32 loc) · 857 Bytes
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
33
34
35
36
37
38
package leetcode.easy.page1;
/**
* @author Kyle
* @create 2018/7/22 23:23
*/
public class PathSum {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) {
return false;
}
sum -= root.val;
if(root.left == null && root.right == null) {
if(sum == 0) {
return true;
} else {
return false;
}
} else if(root.left == null) {
return hasPathSum(root.right, sum);
} else if(root.right == null) {
return hasPathSum(root.left, sum);
} else {
return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
}
}
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}