- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem112.java
More file actions
Latest commit
22 lines (18 loc) · 657 Bytes
/
Copy pathProblem112.java
File metadata and controls
22 lines (18 loc) · 657 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
classProblem112 {
publicstaticvoidmain(String[] args) {
Problem112p = newProblem112();
TreeNoderoot = newTreeNode(1);
root.left = newTreeNode(2);
root.right = newTreeNode(3);
root.left.right = newTreeNode(4);
System.out.println(p.hasPathSum(root, 8));
}
publicbooleanhasPathSum(TreeNoderoot, intsum) {
if (root == null) returnfalse;
if (root.val == sum) {
if (root.left == null && root.right == null) returntrue;
returnfalse;
}
returnhasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);
}
}