-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTApp.java
More file actions
441 lines (248 loc) · 9.17 KB
/
Copy pathBTApp.java
File metadata and controls
441 lines (248 loc) · 9.17 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package trees;
import java.util.LinkedList;
import java.util.Queue;
public class BTApp {
public static int numNodesGreaterX(BinaryTreeNode<Integer> root,int x){
if(root==null){
return 0;
}
int count=(root.data>x)?1:0;
if(root.left!=null){
count+=numNodesGreaterX(root.left,x);
}
if(root.right!=null) {
return numNodesGreaterX(root.right,x);
}
return count;
}
public static int largestNode(BinaryTreeNode<Integer> root){
if(root==null){
return -1;
}
int left=largestNode(root.left);
int right=largestNode(root.right);
return Math.max(root.data,Math.max(left,right));
}
public static int heightBT(BinaryTreeNode<Integer> root) {
if(root==null){
return 0;
}
return 1+Math.max(heightBT(root.left),heightBT(root.right) );
}
public static void helper(BinaryTreeNode<Integer>root,int dep){
if(root==null){
return;
}
root.data=dep;
helper(root.left,dep+1);
helper(root.right,dep+1);
}
public static void inorder(BinaryTreeNode<Integer>root){
if(root==null){
return;
}
inorder(root.left);
System.out.println(root.data);
inorder(root.right);
}
static void replaceDepth(BinaryTreeNode<Integer> n1) {
helper(n1,0);
inorder(n1);
}
public static void printAtDepth(BinaryTreeNode<Integer> n1,int k) {
if(n1== null){
return;
}
if(k==0){
System.out.println(n1.data);
}
printAtDepth(n1.left,k-1);
printAtDepth(n1.right,k-1);
}
public static int nodesAtDistK(BinaryTreeNode<Integer>root,int node,int k){
if(root==null){
return -1;
}
if(root.data==node){
printAtDepth(root,k);
return 0;
}
int ld=nodesAtDistK(root.left,node,k);
if(ld!=-1){
if(ld+1==k){
System.out.println(root.data);
}
else{
printAtDepth(root.right,k-ld-2);
}
return 1+ld;
}
int rd=nodesAtDistK(root.right,node,k);
if(rd!=-1){
if(rd+1==k){
System.out.println(root.data);
}
else{
printAtDepth(root.left,k-rd-2);
}
return 1+rd;
}
return -1;
}
public static int numleaves(BinaryTreeNode<Integer> root) {
if(root==null){
return 0;
}
if(root.left==null && root.right==null){
return 1;
}
return numleaves(root.left)+numleaves(root.right);
}
public static boolean isNodePresent(BinaryTreeNode<Integer> root,int x){
if(root==null)
{
return false;
}
if(root.data==x){
return true;
}
boolean left=isNodePresent(root.left,x);
boolean right=isNodePresent(root.right,x);
return left|| right;
}
public static void printNodesWithoutSibling(BinaryTreeNode<Integer> root) {
if(root==null){
return ;
}
if(root.left!=null && root.right==null){
System.out.println(root.left.data);
}
else if(root.left==null && root.right!=null){
System.out.println(root.right.data);
}
printNodesWithoutSibling(root.left);
printNodesWithoutSibling(root.right);
}
public static void mirror(BinaryTreeNode<Integer> root){
if(root==null){
return ;
}
mirror(root.left);
mirror(root.right);
BinaryTreeNode<Integer> temp=root.left;
root.left=root.right;
root.right=temp;
}
public static Pair helperDiameterTree(BinaryTreeNode<Integer> root){
if(root==null){
return new Pair(0,0);
}
Pair left=helperDiameterTree(root.left);
Pair right=helperDiameterTree(root.right);
//longest dist between deepest nodes left and right subtree
int dist=left.height+right.height+1;
int diameter=Math.max(dist,Math.max(left.diameter,right.diameter));
int height=1+Math.max(left.height,right.height);
return new Pair(height,diameter);
}
public static void printLevelWise(BinaryTreeNode<Integer> root) {
if(root==null){
return ;
}
Queue<BinaryTreeNode<Integer>> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
BinaryTreeNode<Integer>front=q.poll();
System.out.print(front.data+":");
if(front.left!=null){
System.out.print("L:"+front.left.data+",");
q.add(front.left);
}
else{
System.out.print("L:"+"-1"+",");
}
if(front.right!=null){
System.out.println("R:"+front.right.data);
q.add(front.right);
}
else{
System.out.println("R:"+"-1");
}
}
}
public static BinaryTreeNode<Integer>helper(int[] pre, int presi, int preei, int[] in, int insi, int inei){
if(preei<presi){
return null;
}
BinaryTreeNode<Integer>root=null;
int rootdata=pre[presi];
if(rootdata!=-1){
root= new BinaryTreeNode<>(rootdata);
}
int i;
for(i=insi;i<=inei;i++){
if(rootdata==in[i]){
break;
}
}
int leftpresi=presi+1;
int leftinsi=insi;
int leftinei=i-1;
int rightpreei=preei;
int rightinsi=i+1;
int rightinei=inei;
int len=leftinei-leftinsi+1;
int leftpreei=leftpresi+len-1;
int rightpresi=leftpreei+1;
root.left=helper(pre,leftpresi,leftpreei,in,leftinsi,leftinei);
root.right=helper(pre,rightpresi,rightpreei,in,rightinsi,rightinei);
return root;
}
public static BinaryTreeNode<Integer> getTreeFromPreorderAndInorder(int[] pre,
int[] in){
return helper(pre,0,pre.length-1,in,0,in.length-1);
}
public static BinaryTreeNode<Integer>helper11(int post[],int postsi,int postei,int in[],int insi,int inei){
if(postei<postsi){
return null;
}
BinaryTreeNode<Integer>root=null;
int rootdata=post[postei];
if(rootdata!=-1){
root=new BinaryTreeNode<Integer>(rootdata);
}
int i;
for(i=insi;i<=inei;i++){
if(rootdata==in[i]){
break;
}
}
int leftpostsi=postsi;
int leftinsi=insi;
int leftinei=i-1;
int rightpostei=postei-1;
int rightinsi=i+1;
int rightinei=inei;
int len=leftinei-leftinsi+1;
int leftpostei=leftpostsi+len-1;
int rightpostsi=leftpostei+1;
root.left=helper(post,leftpostsi,leftpostei,in,leftinsi,leftinei);
root.right=helper(post,rightpostsi,rightpostei,in,rightinsi,rightinei);
return root;
}
public static BinaryTreeNode<Integer> getTreeFromPostorderAndInorder(int[] post,
int[] in){
return helper11(post,0,post.length-1,in,0,in.length-1);
}
public static void insertDuplicateNode(BinaryTreeNode<Integer> root) {
if(root==null){
return ;
}
BinaryTreeNode<Integer>copy=new BinaryTreeNode<>(root.data);
BinaryTreeNode<Integer>temp=root.left;
root.left=copy;
copy.left=temp;
insertDuplicateNode(copy.left);
insertDuplicateNode(root.right);
}
}