-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSum.java
More file actions
43 lines (39 loc) · 881 Bytes
/
Copy pathPathSum.java
File metadata and controls
43 lines (39 loc) · 881 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
39
40
41
42
43
package trees;
class Node{
int data;
Node left;
Node right;
Node(int x){
data=x;
left=null;
right=null;
}
}
public class PathSum {
Node root;
boolean hasPathSum(Node node, int sum)
{
boolean res=false;
Node curr=root;
int leftSum=0,rightSum=0;
while(curr!=null){
leftSum+=curr.data;
curr=curr.left;
}
curr=root;
while(curr!=null){
rightSum+=curr.data;
curr=curr.right;
}
if((leftSum==sum) || (rightSum==sum))res=true;
return res;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PathSum ps = new PathSum();
ps.root = new Node(1);
ps.root.left = new Node(2);
ps.root.right = new Node(3);
System.out.println(ps.hasPathSum(ps.root, 4));
}
}