Repository navigation
Expand file tree
/
Copy pathBinaryTree.java
More file actions
232 lines (218 loc) · 5.38 KB
/
Copy pathBinaryTree.java
File metadata and controls
232 lines (218 loc) · 5.38 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
import java.util.*;
import java.util.LinkedList;
import java.io.*;
class Node{
Node left;
Node right;
int val;
}
public class BinaryTree {
public int size(Node root)
{
if(root==null)
return 0;
int ls = size(root.left);
int rs = size(root.right);
return ls+rs+1;
}
public int sum(Node root)
{
if(root==null)
return 0;
int ls = sum(root.left);
int rs = sum(root.right);
return ls+rs+root.val;
}
public int maximum(Node root)
{
if(root==null)
return Integer.MIN_VALUE;
int lmax=maximum(root.left);
int rmax=maximum(root.right);
int cmax=Math.max(lmax,rmax);
int mmax=Math.max(cmax,root.val);
return mmax;
}
public int minimum(Node root)
{
if(root==null)
return Integer.MAX_VALUE;
int lmin=minimum(root.left);
int rmin=minimum(root.right);
int cmin=Math.min(lmin,rmin);
int mmin=Math.min(cmin,root.val);
return mmin;
}
public boolean isPresent(Node root,int key)
{
if(root==null)
return false;
if(root.val==key)
return true;
boolean lans=isPresent(root.left,key);
boolean rans=isPresent(root.right,key);
if(lans || rans)
return true;
return false;
}
public boolean isPresent(Node root,int key,ArrayList<Node>path)
{
if(root==null)
return false;
if(root.val==key)
{
path.add(root);
return true;
}
boolean lans=isPresent(root.left,key,path);
if(lans==true)
{
path.add(root);
return true;
}
boolean rans=isPresent(root.right,key,path);
if(rans == true)
{
path.add(root);
return true;
}
return false;
}
Node lca=null;
public boolean isPresent(Node root,int key,Set<Node>path)
{
if(root==null)
return false;
if(root.val==key)
{
if(path.contains(root))
{
lca=root;
return true;
}
path.add(root);
return true;
}
boolean lans=isPresent(root.left,key,path);
if(lans==true)
{
path.add(root);
return true;
}
boolean rans=isPresent(root.right,key,path);
if(rans == true)
{
path.add(root);
return true;
}
return false;
}
public void LCA_setApproach(Node root,Node n1,Node n2)
{
Set<Node>path1=new HashSet<>();
isPresent(root,n1.val, path1);
isPresent(root,n2.val,path1);
}
public Node LCA(Node root,Node n1,Node n2)
{
ArrayList<Node>path1=new ArrayList<>();
ArrayList<Node>path2=new ArrayList<>();
isPresent(root,n1.val,path1);
isPresent(root,n2.val,path2);
int i=path1.size()-1;
int j=path2.size()-1;
Node lca=null;
while(i>=0 && j>=0)
{
if(path1.get(i).val == path2.get(j).val)
{
i--;
j--;
}
else
{
lca=path1.get(i+1);
break;
}
}
return lca;
}
public void printInorder(Node root)
{
if(root==null)
return;
printInorder(root.left);
System.out.print(root.val+" ");
printInorder(root.right);
}
public void printPreorder(Node root)
{
if(root==null)
return;
System.out.print(root.val+" ");
printPreorder(root.left);
printPreorder(root.right);
}
public void printPostorder(Node root)
{
if(root==null)
return;
printPostorder(root.left);
printPostorder(root.right);
System.out.print(root.val+" ");
}
public void printLevelOrderSingleLine(Node root)
{
Queue<Node>q=new LinkedList();
q.add(root);
while(q.size()>0)
{
Node fnt=q.remove();
System.out.print(fnt.val+" ");
if(fnt.left!=null)
q.add(fnt.left);
if(fnt.right!=null)
q.add(fnt.right);
}
}
public void printLevelOrderLevelWise(Node root)
{
Queue<Node>q=new LinkedList();
q.add(root);
while(q.size()>0)
{
int s=q.size();
while(s>0)
{
Node fnt=q.remove();
System.out.print(fnt.val+" ");
if(fnt.left!=null)
q.add(fnt.left);
if(fnt.right!=null)
q.add(fnt.right);
s--;
}
System.out.println();
}
}
public int height_Edge(Node root)
{
if(root==null)
return -1;
int lh=height_Edge(root.left);
int rh=height_Edge(root.right);
int mh=Math.max(lh,rh)+1;
return mh;
}
public int height_Node(Node root)
{
if(root==null)
return 0;
int lh=height_Node(root.left);
int rh=height_Node(root.right);
int mh=Math.max(lh,rh)+1;
return mh;
}
public static void main(String[] args) {
}
}