- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath_Sum.java
More file actions
Latest commit
22 lines (20 loc) · 535 Bytes
/
Copy pathPath_Sum.java
File metadata and controls
22 lines (20 loc) · 535 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
publicclassSolution {
publicbooleanhasPathSum(TreeNoderoot, intsum) {
if (root == null) {
returnfalse;
}
if ((root.left == null) && (root.right == null)) {
return (sum == root.val);
}
returnhasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);
}
}