-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.java
More file actions
137 lines (107 loc) · 2.67 KB
/
Copy pathbst.java
File metadata and controls
137 lines (107 loc) · 2.67 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.List;
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
class Node {
int val;
Node left;
Node right;
public Node(int v) {
val = v;
}
}
class bst {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Not enough arguments!");
return;
}
bst solution = new bst();
solution.driver(args);
}
void driver(String[] args) {
Node n = new Node(5);
n.left = new Node(3);
n.right = new Node(7);
n.left.left = new Node(1);
n.left.left.right = new Node(2);
n.right.left = new Node(6);
n.right.right = new Node(8);
printPreOrderTree(n);
switch (args[0]) {
case "ksumpathscount":
System.out.println(kSumPathsCount(n, Integer.parseInt(args[1])));
break;
case "ksumpaths":
List<List<Integer>> answer = kSumPaths(n, Integer.parseInt(args[1]));
for (List<Integer> l : answer) {
for (int i : l) {
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Error while parsing input.");
}
}
void printPreOrderTree(Node node) {
Queue<Node> q = new LinkedList<>();
q.add(node);
Node n = null;
while (!q.isEmpty()) {
n = q.remove();
if (n != null) {
q.add(n.left);
q.add(n.right);
System.out.print(n.val + " ");
} else System.out.print("null ");
}
System.out.println();
}
int kSumPathsCount(Node root, int k) {
return kSumPathsCountHelper(root, k, new ArrayList<Node>());
}
int kSumPathsCountHelper(Node root, int k, List<Node> path) {
if (root == null) return 0;
int count = 0;
path.add(root);
count += kSumPathsCountHelper(root.left, k, path)
+ kSumPathsCountHelper(root.right, k, path);
int sum = 0;
for (int i = path.size() - 1; i >= 0; i--) {
sum += path.get(i).val;
if (sum == k) {
count++;
}
}
path.remove(path.size() - 1);
return count;
}
List<List<Integer>> kSumPaths(Node root, int k) {
List<List<Integer>> result = new ArrayList<>();
kSumPathsHelper(root, k, new ArrayList<Node>(), result);
return result;
}
void kSumPathsHelper(Node root, int k, List<Node> path, List<List<Integer>> result) {
if (root == null) return;
path.add(root);
kSumPathsHelper(root.left, k, path, result);
kSumPathsHelper(root.right, k, path, result);
int sum = 0;
for (int i = path.size() - 1; i >= 0; i--) {
sum += path.get(i).val;
if (sum == k) {
result.add(getSubList(path, i));
}
}
path.remove(path.size() - 1);
}
List<Integer> getSubList(List<Node> list, int start) {
List<Integer> result = new ArrayList<>();
for(int i = start; i<list.size(); i++) {
result.add(list.get(i).val);
}
return result;
}
}