-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSum.java
More file actions
61 lines (51 loc) · 1.28 KB
/
PathSum.java
File metadata and controls
61 lines (51 loc) · 1.28 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package chapter4TreesAndGraphs;
import java.util.ArrayList;
public class PathSum {
static class Node {
int data;
Node left;
Node right;
public Node (int data, Node left, Node right){
this.data = data;
this.left = left;
this.right = right;
}
public Node(int data) {
this.data = data;
left = null;
right = null;
}
}
public static void main(String[] args){
Node n5 = new Node(1,null,null);
Node n6 = new Node(2,null,null);
Node n3 = new Node(2,null,null);
Node n4 = new Node(5,null,null);
Node n1 = new Node(3,n3,n4);
Node n2 = new Node(4,n5,n6);
Node n0 = new Node(1,n1,n2);
ArrayList<Node> lst = new ArrayList<Node>();
findPath(n0, 6, 0, lst);
}
static void findPath(Node root, int sum, int level, ArrayList<Node> lst){
if(root==null) return;
lst.add(root);
int tmp=0;
for(int i=level;i>-1;i--){
tmp +=lst.get(i).data;
if(tmp==sum) print(lst,i,level);
}
ArrayList<Node> l = new ArrayList<Node>();
ArrayList<Node> r = new ArrayList<Node>();
l.addAll(lst);
r.addAll(lst);
findPath(root.left,sum,level+1,l);
findPath(root.right,sum,level+1,r);
}
static void print(ArrayList<Node> lst, int i, int level){
for(int j =i;j<level+1;j++){
System.out.print(lst.get(j).data+" ");
}
System.out.println();
}
}