Repository navigation
Expand file tree
/
Copy pathBST.java
More file actions
195 lines (179 loc) · 4.96 KB
/
Copy pathBST.java
File metadata and controls
195 lines (179 loc) · 4.96 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
public class BST {
public int sumBST(Node root)
{
if (root == null)
return 0;
int ls = sumBST(root.left);
int rs = sumBST(root.right);
return ls + rs + root.val;
}
public int sizeBST(Node root)
{
if(root==null)
return 0;
int ls = sizeBST(root.left);
int rs = sizeBST(root.right);
return ls+rs+1;
}
public int maximumBST(Node root)
{
if(root==null)
return Integer.MIN_VALUE;
int lmax=maximumBST(root.left);
int rmax=maximumBST(root.right);
int cmax=Math.max(lmax,rmax);
int mmax=Math.max(cmax,root.val);
return mmax;
}
public int maximumBST2(Node root)
{
if(root==null)
return Integer.MIN_VALUE;
while(root.right!=null)
root=root.right;
return root.val;
}
public int minimumBST2(Node root)
{
if(root==null)
return Integer.MIN_VALUE;
while(root.left!=null)
root=root.left;
return root.val;
}
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 isPresent2(Node root,int key)
{
if(root==null)
return false;
if(root.val==key)
return true;
if(root.val>key)
return isPresent2(root.left, key);
else
return isPresent2(root.right, key);
}
public Node LCABst(Node root,int n1,int n2)
{
Node curr=root;
while(curr!=null)
{
if(curr.val> n1 && curr.val >n2)
curr=curr.left;
else if(curr.val<n1 && curr.val <n2)
curr=curr.right;
else
return curr;
}
return null;
}
public Node LCABstRec(Node root,int n1,int n2)
{
if(root==null)
return null;
if((root.val>=n1 && root.val<=n2) || (root.val>=n2 && root.val<=n1))
return root;
if(root.val>n1 && root.val>n2)
return LCABstRec(root.left,n1,n2);
else if(root.val<n2 && root.val<n1)
return LCABstRec(root.right,n1,n2);
return null;
}
public class data
{
long min;
long max;
boolean isBST;
data(long min,long max,boolean isBST)
{
this.min=min;
this.max=max;
this.isBST=isBST;
}
}
public static data isBST(Node root)
{
if(root==null)
{
data nullcase=new data(Long.MIN_VALUE, Integer.MAX_VALUE, true);
return nullcase;
}
data ldata=isBST(root.left);
data rdata=isBST(root.right);
boolean c1= ldata.max<root.val;
boolean c2=rdata.min>root.val;
boolean c3=ldata.isBST;
boolean c4=rdata.isBST;
boolean myisBst=c1&&c2&&c3&&c4;
long mymin=Math.min(root.val,Math.min(ldata.min,rdata.min));
long mymax=Math.max(root.val,Math.max(ldata.max,rdata.max));
data mydata=new data(mymin, mymax, myisBst);
return mydata;
}
public static boolean isTreeBST(Node root)
{
data d=isBST(root);
return d.isBST;
}
public boolean isTreeBST2(Node root)
{
return isBSTHelper(root,Long.MIN_VALUE,Long.MAX_VALUE);
}
public boolean isBSTHelper(Node root,long lowerLimit, long UpperLimit)
{
if(root==null)
return true;
if(root.val>UpperLimit || root.val<lowerLimit)
return false;
boolean lans=isBSTHelper(root.left, lowerLimit, root.val-1);
boolean rans=isBSTHelper(root.right, root.val+1, UpperLimit);
return lans&& rans;
}
public Node insertInBST(Node root,int key)
{
if(root==null)
return new Node(key);
if(root.val>key)
root.left=insertInBST(root.left,key);
else
root.right=insertInBST(root.right,key);
return root;
}
public Node removeFromBST(Node root,int key)
{
if(root==null)
return null;
if(root.val>key)
{
Node lans=removeFromBST(root.left,key);
root.left=lans;
}
if(root.val<key)
{
Node rans=removeFromBST(root.right,key);
root.right=rans;
}
if(root.val==key)
{
if(root.left==null || root.right==null)
return root.left==null?root.right:root.left;
Node replaceNode=maximumBST(root.left);
root.val=replaceNode.val;
// IMPORTANT STEP
root.left=removeFromBST(root.left, replaceNode.val);
}
}
public static void main(String[] args) {
}
}