-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter4.java
407 lines (357 loc) · 11.3 KB
/
Chapter4.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* Cracking the Coding Interview Chapter 4
* Grace Guan 8/30/17
*
* Trees and Graphs
*/
import java.util.*;
import java.io.*;
public class Chapter4 {
private void dfs(Node root) {
if (root == null) return;
visit(root);
root.visited = true;
for (Node n : root.adjacent) {
if (! n.visited) dfs(n);
}
}
private void bfs(Node root) {
Queue q = new Queue();
root.marked = true;
queue.enqueue(root);
while (! queue.isEmpty()) {
Node r = queue.dequeue();
visit(r);
for (Node n : r.adjacent) {
if (! n.marked) {
n.marked = true;
queue.enqueue(n);
}
}
}
}
// 4.1 whether there's a route between two nodes
// do bfs, if node n == node end, return true
private boolean isPath(Node root, Node end) {
Queue q = new Queue();
root.marked = true;
queue.enqueue(root);
while (! queue.isEmpty()) {
Node r = queue.dequeue();
if (r == end) return true;
visit(r);
for (Node n : r.adjacent) {
if (! n.marked) {
n.marked = true;
queue.enqueue(n);
}
}
}
return false;
}
// 4.2 minimal tree - use recursion, start from middle
private static TreeNode createMinimalBST(int arr[], int start, int end){
if (end < start) {
return null;
}
int mid = (start + end) / 2;
TreeNode n = new TreeNode(arr[mid]);
n.setLeftChild(createMinimalBST(arr, start, mid - 1));
n.setRightChild(createMinimalBST(arr, mid + 1, end));
return n;
}
public static TreeNode createMinimalBST(int array[]) {
return createMinimalBST(array, 0, array.length - 1);
}
// 4.3 use BFS
public static ArrayList<LinkedList<TreeNode>> createLevelLinkedList(TreeNode root) {
ArrayList<LinkedList<TreeNode>> result = new ArrayList<LinkedList<TreeNode>>();
/* "Visit" the root */
LinkedList<TreeNode> current = new LinkedList<TreeNode>();
if (root != null) {
current.add(root);
}
while (current.size() > 0) {
result.add(current); // Add previous level
LinkedList<TreeNode> parents = current; // Go to next level
current = new LinkedList<TreeNode>();
for (TreeNode parent : parents) {
/* Visit the children */
if (parent.left != null) {
current.add(parent.left);
}
if (parent.right != null) {
current.add(parent.right);
}
}
}
return result;
}
// 4.4
public int checkHeight(TreeNode root) {
if (root == null) return -1;
int leftHeight = checkHeight(root.left);
if (leftHeight == Integer.MIN_VALUE) return Integer.MIN_VALUE; // Propagate error up
int rightHeight = checkHeight(root.right);
if (rightHeight == Integer.MIN_VALUE) return Integer.MIN_VALUE; // Propagate error up
int heightDiff = leftHeight - rightHeight;
if (Math.abs(heightDiff) > 1) {
return Integer.MIN_VALUE; // Found error -> pass it back
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
public boolean isBalanced(TreeNode root) {
return checkHeight(root) != Integer.MIN_VALUE;
}
// 4.5
public boolean validateBST(TreeNode root) {
return validateBST(root.left, root.value, true) && validateBST(root.right, root.value, false);
}
public boolean validateBST(TreeNode node, int oldValue, boolean isLeft) {
if (node == null) return true;
if (!validateBST(node.left, node.value, true)) {
return false;
}
if (oldValue != null) {
if (isLeft && n.value < oldValue) return false;
if (!isLeft && n.value <= oldValue) return false;
}
if (!validateBST(node.right, node.value, false)) {
return false;
}
return true;
}
// 4.6
public Node successor(TreeNode root) {
if (root == null) return null;
Node n = root;
// Found right children -> return left most node of right subtree
if (n.parent == null || n.right != null) {
while (n.right != null) {
n = n.right;
}
return n;
} else {
TreeNode q = n;
TreeNode x = q.parent;
// Go up the left side
while (x != null && x.left != q) {
q = x;
x = x.parent;
}
return x;
}
}
// 4.7
public class CharPair {
public char first;
public char second;
public CharacterPair(char first, char second) {
this.first = first;
this.second = second;
}
}
public class CharNode{
public char c;
public ArrayList<CharNode> outgoing;
public ArrayList<CharNode> incoming;
public CharNode(char c) {
this.c = c;
adjacent = new ArrayList<CharNode>();
}
public void addOutgoing(CharNode c) {
outogoing.add(c);
}
public void addIncoming(CharNode c) {
incoming.add(c);
}
public boolean allValidIncoming(Hashtable<CharNode, Boolean> marked) {
for (CharNode c : incoming) {
if (marked.get(c) == false) return false;
}
return true;
}
}
public ArrayList<Character> buildOrder(ArrayList<Character> projects, ArrayList<CharacterPair> dependencies) {
Hashtable<Character, CharNode> chars = new Hashtable<Character, CharNode>();
Hashtable<CharNode, Boolean> marked = new Hashtable<CharNode, Boolean>();
for (Character c : projects) {
marked.add(new CharNode(c), false);
}
for (CharacterPair p : dependencies) {
chars.get(p.first).addOutgoing(chars.get(p.second));
chars.get(p.second).addIncoming(chars.get(p.first));
}
// bfs
ArrayList<Character> buildOrder = new ArrayList<Character>();
Queue<CharNode> q = new Queue<CharNode>();
for (CharNode c : marked) {
if (c.incoming.size() == 0) {
marked.put(c, true);
q.enqueue(c);
buildOrder.add(c.c);
}
}
while (! q.isEmpty()) {
CharNode c = q.dequeue();
for (CharNode c2 : c.outgoing) {
if (marked.get(c2) == false && c2.allValidIncoming(marked)) {
q.enqueue(c2);
marked.put(c2, true);
buildOrder.add(c2.c);
}
}
} // or pop to a stack
if (buildOrder.size() != projects.size()) {
return null;
}
return buildOrder;
}
// 4.8
public static TreeNode commonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (!covers(root, p) || !covers(root, q)) { // Error check - one node is not in tree
return null;
}
return ancestorHelper(root, p, q);
}
public static TreeNode ancestorHelper(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
boolean pIsOnLeft = covers(root.left, p);
boolean qIsOnLeft = covers(root.left, q);
if (pIsOnLeft != qIsOnLeft) { // Nodes are on different side
return root;
}
TreeNode childSide = pIsOnLeft ? root.left : root.right;
return ancestorHelper(childSide, p, q);
}
// check if root is a parent of p
public static boolean covers(TreeNode root, TreeNode p) {
if (root == null) return false;
if (root == p) return true;
return covers(root.left, p) || covers(root.right, p);
}
// 4.9 ???????????????? review, do not understand
public static void weaveLists(LinkedList<Integer> first, LinkedList<Integer> second, ArrayList<LinkedList<Integer>> results, LinkedList<Integer> prefix) {
/* One list is empty. Add the remainder to [a cloned] prefix and
* store result. */
if (first.size() == 0 || second.size() == 0) {
LinkedList<Integer> result = (LinkedList<Integer>) prefix.clone();
result.addAll(first);
result.addAll(second);
results.add(result);
return;
}
/* Recurse with head of first added to the prefix. Removing the
* head will damage first, so we’ll need to put it back where we
* found it afterwards. */
int headFirst = first.removeFirst();
prefix.addLast(headFirst);
weaveLists(first, second, results, prefix);
prefix.removeLast();
first.addFirst(headFirst);
/* Do the same thing with second, damaging and then restoring
* the list.*/
int headSecond = second.removeFirst();
prefix.addLast(headSecond);
weaveLists(first, second, results, prefix);
prefix.removeLast();
second.addFirst(headSecond);
}
public static ArrayList<LinkedList<Integer>> allSequences(TreeNode node) {
ArrayList<LinkedList<Integer>> result = new ArrayList<LinkedList<Integer>>();
if (node == null) {
result.add(new LinkedList<Integer>());
return result;
}
LinkedList<Integer> prefix = new LinkedList<Integer>();
prefix.add(node.data);
/* Recurse on left and right subtrees. */
ArrayList<LinkedList<Integer>> leftSeq = allSequences(node.left);
ArrayList<LinkedList<Integer>> rightSeq = allSequences(node.right);
/* Weave together each list from the left and right sides. */
for (LinkedList<Integer> left : leftSeq) {
for (LinkedList<Integer> right : rightSeq) {
ArrayList<LinkedList<Integer>> weaved = new ArrayList<LinkedList<Integer>>();
weaveLists(left, right, weaved, prefix);
result.addAll(weaved);
}
}
return result;
}
// 4.10 all we have to do is check the inorder traversals
public void getInOrder(Node n, StringBuilder sb) {
if (node == null) {
sb.append("X");
return;
}
sb.append(n.data);
getInOrder(n.left, sb);
getInOrder(n.right, sb);
}
public boolean isSubtree(Node n1, Node n2) {
StringBuilder string1 = new StringBuilder();
StringBuilder string2 = new StringBuilder();
getInOrder(n1, string1);
getInOrder(n2, string2);
return string1.toString().indexOf(string2.toString()) != -1;
}
// 4.11
// find the ith node of a tree
public TreeNode getIthNode(int i) {
int leftSize = left == null ? 0 : left.size();
if (i < leftSize) {
return left.getIthNode(i);
} else if (i == leftSize) {
return this;
} else {
return right.getIthNode(i - (leftSize + 1));
}
}
// 4.12 ??? review again
public static int countPathsWithSum(TreeNode root, int targetSum) {
if (root == null) return 0;
/* Count paths with sum starting from the root. */
int pathsFromRoot = countPathsWithSumFromNode(root, targetSum, 0);
/* Try the nodes on the left and right. */
int pathsOnLeft = countPathsWithSum(root.left, targetSum);
int pathsOnRight = countPathsWithSum(root.right, targetSum);
return pathsFromRoot + pathsOnLeft + pathsOnRight;
}
/* Returns the number of paths with this sum starting from this node. */
public static int countPathsWithSumFromNode(TreeNode node, int targetSum, int currentSum) {
if (node == null) return 0;
currentSum += node.data;
int totalPaths = 0;
if (currentSum == targetSum) { // Found a path from the root
totalPaths++;
}
totalPaths += countPathsWithSumFromNode(node.left, targetSum, currentSum); // Go left
totalPaths += countPathsWithSumFromNode(node.right, targetSum, currentSum); // Go right
return totalPaths;
}
// interview game
public void game(int[] input){
int n = input[0];
for(int t = 1; t < n; t++) {
// change int to binary
char[] chars = Integer.toBinaryString(input[t]).toCharArray();
// count inversions
int inversions = 0;
for (int i = 0; i < chars.length; i++) {
for (int j = i; j < chars.length; j++) {
if (chars[i] > chars[j]) {
inversions++;
}
}
}
if (inversions % 2 == 0) {
System.out.println("Second Player");
} else {
System.out.println("First Player");
}
}
}
}