-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath Sum.java
47 lines (44 loc) · 1.36 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
// Start typing your Java solution below
// DO NOT write main() function
if(root==null) return false;
if(root.left==null&&root.right==null&&root.val==sum)
return true;
else return hasPathSum(root.right,sum-root.val) || hasPathSum(root.left,sum-root.val);
}
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
// Start typing your Java solution below
// DO NOT write main() function
if(root==null) return false;
Queue<TreeNode> res=new LinkedList<TreeNode>();
Queue<Integer> resum=new LinkedList<Integer>();
res.add(root);
resum.add(root.val);
while(res.size()!=0)
{
TreeNode r=res.poll();
Integer s=resum.poll();
if(r.left==null&&r.right==null&&s==sum) return true;
if(r.left!=null){
res.add(r.left);
resum.add(r.left.val+s);
}
if(r.right!=null){
res.add(r.right);
resum.add(r.right.val+s);
}
}
return false;
}
}