-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
388 lines (355 loc) · 11.4 KB
/
BinaryTree.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
import java.util.*;
import utils.TreeNode;
public class BinaryTree {
/**
* Pre-order Traversal Of Binary Tree (recursive)
* <p>
* Implement a recursive, pre-order traversal of a given binary tree, return the
* list of keys of each node in the tree as it is pre-order traversed.
*/
public List<Integer> preOrder(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
preOrder(root, result);
return result;
}
private void preOrder(TreeNode node, List<Integer> result) {
if (node == null) {
return;
}
result.add(node.key);
preOrder(node.left, result);
preOrder(node.right, result);
}
/**
* Height of Binary Tree
* <p>
* Find the height of binary tree
*/
public int findHeight(TreeNode root) {
if (root == null) {
return 0;
}
int h1 = findHeight(root.left);
int h2 = findHeight(root.right);
return Math.max(h1, h2) + 1;
}
/**
* Check If Binary Tree Is Balanced
* <p>
* Check if a given binary tree is balanced. A balanced binary tree is one in
* which the depths of every node’s left and right subtree differ by at most 1.
*/
public boolean isBalanced(TreeNode root) {
return findIfBalanced(root) != -1;
}
private int findIfBalanced(TreeNode root) {
if (root == null) {
return 0;
}
int h1 = findIfBalanced(root.left);
int h2 = findIfBalanced(root.right);
if (h1 == -1 || h2 == -1 || Math.abs(h1 - h2) > 1) {
return -1;
}
return Math.max(h1, h2) + 1;
}
/**
* Symmetric Binary Tree
* <p>
* Check if a given binary tree is symmetric
*/
public boolean isSymmetric(TreeNode root) {
return isSymmetric(root, root);
}
private boolean isSymmetric(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null || left.key != right.key) {
return false;
}
return isSymmetric(left.right, right.left) && isSymmetric(left.left, right.right);
}
/**
* Tweaked Identical Binary Trees
* <p>
* Determine whether two given binary trees are identical assuming any number of
* ‘tweak’s are allowed. A tweak is defined as a swap of the children of one
* node in the tree.
*/
public boolean isTweakedIdentical(TreeNode one, TreeNode two) {
if (one == null && two == null) {
return true;
}
if (one == null || two == null || one.key != two.key) {
return false;
}
return (isTweakedIdentical(one.left, two.left) && isTweakedIdentical(one.right, two.right)
|| isTweakedIdentical(one.left, two.right) && isTweakedIdentical(one.right, two.left));
}
/**
* Is Binary Search Tree Or Not
* <p>
* Determine if a given binary tree is binary search tree.There should no be
* duplicate keys in binary search tree.
*/
public boolean isBST(TreeNode root) {
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
return isBST(root, min, max);
}
private boolean isBST(TreeNode node, int min, int max) {
if (node == null) {
return true;
}
if (node.key >= max || node.key <= min) {
return false;
}
return isBST(node.left, min, node.key) && isBST(node.right, node.key, max);
}
/**
* Get Keys In Binary Search Tree In Given Range
* <p>
* Get the list of keys in a given binary search tree in a given range[min, max]
* in ascending order, both min and max are inclusive.
*/
public List<Integer> getRange(TreeNode root, int min, int max) {
List<Integer> result = new ArrayList<>();
getRange(root, min, max, result);
return result;
}
private void getRange(TreeNode node, int min, int max, List<Integer> result) {
if (min > max) {
return;
}
if (node == null) {
return;
}
getRange(node.left, min, Math.min(node.key, max), result);
if (node.key >= min && node.key <= max) {
result.add(node.key);
}
getRange(node.right, Math.max(node.key, min), max, result);
}
/**
* Search In Binary Search Tree
* <p>
* Find the target key K in the given binary search tree, return the node that
* contains the key if K is found, otherwise return null.
*/
public TreeNode search(TreeNode root, int key) {
if (root == null) {
return null;
}
if (root.key == key) {
return root;
}
if (root.key > key) {
return search(root.left, key);
} else {
return search(root.right, key);
}
}
/**
* Insert in Binary Search Tree
* <p>
* Insert a key in a binary search tree if the binary search tree does not
* already contain the key. Return the root of the binary search tree.
*/
// public TreeNode insert(TreeNode root, int key) {
// if (root == null) {
// return new TreeNode(key);
// }
// insertHelper(root, key);
// return root;
// }
// private void insertHelper(TreeNode node, int key) {
// if (node == null || node.key == key) {
// return;
// }
// if (node.key > key) {
// if (node.left == null) {
// node.left = new TreeNode(key);
// } else {
// insertHelper(node.left, key);
// }
// } else {
// if (node.right == null) {
// node.right = new TreeNode(key);
// } else {
// insertHelper(node.right, key);
// }
// }
// }
public TreeNode insert(TreeNode root, int key) {
if (root == null) {
return new TreeNode(key);
}
if (root.key < key) {
root.right = insert(root.right, key);
} else if (root.key > key) {
root.left = insert(root.left, key);
}
return root;
}
/**
* Delete In Binary Search Tree
* <p>
* Delete the target key K in the given binary search tree if the binary search
* tree contains K. Return the root of the binary search tree. Find your own way
* to delete the node from the binary search tree, after deletion the binary
* search tree's property should be maintained.
*/
public TreeNode deleteTree(TreeNode root, int key) {
if (root == null) {
return null;
}
if (root.key > key) {
root.left = deleteTree(root.left, key);
return root;
}
if (root.key < key) {
root.right = deleteTree(root.right, key);
return root;
}
if (root.right == null) {
return root.left;
}
if (root.right.left == null) {
root.right.left = root.left;
return root.right;
}
TreeNode node = root.right;
while (node.left.left != null) {
node = node.left;
}
TreeNode newRoot = node.left;
node.left = newRoot.right; // !!!!IMPORTANT
newRoot.left = root.left;
newRoot.right = root.right;
return newRoot;
}
/**
* Pre-order Traversal of Binary Tree (iterative)
* <p>
* Implement an iterative, pre-order traversal of a given binary tree, return
* the list of keys of each node in the tree as it is pre-order traversed.
*/
public List<Integer> preOrder2(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<TreeNode> stack = new ArrayDeque<>();
stack.offerLast(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pollLast();
result.add(node.key);
if (node.right != null) {
stack.offerLast(node.right);
}
if (node.left != null) {
stack.offerLast(node.left);
}
}
return result;
}
/**
* In-order Traversal of Binary Tree (iterative)
* <p>
* Implement an iterative, in-order traversal of a given binary tree, return the
* list of keys of each node in the tree as it is in-order traversed.
*/
public List<Integer> inOrder(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<TreeNode> stack = new ArrayDeque<>();
stack.offerLast(root);
TreeNode helper = root.left;
TreeNode node;
while (helper != null || !stack.isEmpty()) {
if (helper == null) {
node = stack.pollLast();
result.add(node.key);
helper = node.right;
} else {
stack.offerLast(helper);
helper = helper.left;
}
}
return result;
}
/**
* Post-order Traversal of Binary Tree (iterative)
* <p>
* Implement an iterative, post-order traversal of a given binary tree, return
* the list of keys of each node in the tree as it is post-order traversed.
*/
public List<Integer> postOrder(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode helper = root;
TreeNode node;
while (helper != null || !stack.isEmpty()) {
if (helper == null) {
node = stack.peekLast();
if (node.right != null) {
stack.offerLast(node.right);
helper = node.right.left;
} else {
result.add(node.key);
helper = null;
if (!stack.isEmpty() && stack.peekLast().right == node) {
helper = node;
}
}
} else if (helper == stack.peekLast().right) {
node = stack.pollLast();
result.add(node.key);
helper = null;
if (!stack.isEmpty() && stack.peekLast().right == node) {
helper = node;
}
} else {
stack.offerLast(helper);
helper = helper.left;
}
}
return result;
}
public List<Integer> postOrder2(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<TreeNode> stack = new ArrayDeque<>();
stack.offerLast(root);
TreeNode helper = root;
while (!stack.isEmpty()) {
TreeNode cur = stack.peekLast();
if (cur.right == helper || cur.left == helper && cur.right == null) {
result.add(cur.key);
helper = cur;
} else {
if (cur.right != null) {
stack.offerLast(cur.right);
}
if (cur.left != null) {
stack.offerLast(cur.left);
}
if (cur.left == null && cur.right == null) {
helper = null;
}
}
}
return result;
}
}