-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
257 lines (191 loc) · 5.17 KB
/
tree.cpp
File metadata and controls
257 lines (191 loc) · 5.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
// Question Finding Diameter of Binary Tree
int diameter(struct Node* tree);
int main() {
int t;
scanf("%d\n", &t);
while (t--) {
string s;
getline(cin, s);
Node* root = buildTree(s);
cout << diameter(root) << endl;
}
return 0;
}
/* Tree node structure used in the program
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
}; */
int dia(Node * node, int *h){
if (node==NULL)
return 0;
int lh=0, rh=0;
int ldiameter=dia(node->left, &lh);
int rdiameter=dia(node->right, &rh);
*h=max(lh, rh)+1;
return max(lh+rh+1, max(ldiameter, rdiameter));
}
int diameter(Node* node) {
// Your code here
int h=0;
return dia(node, &h);
}
// Question : Top View of tree
vector<int> topView(struct Node *root)
{vector<int> result;
if (root==NULL)
return result;
map<struct Node*, int> s;
map<int, int>r;
queue<struct Node*>q;
q.push(root);
int hd=0;
struct Node* x=root;
s[root]=hd;
while(!q.empty()){
hd=s[x];
if(r.count(hd)==0)
{
r[hd]=x->data;
}
if(x->left){
s[x->left]=hd-1;
q.push(x->left);
}
if(x->right){
s[x->right]=hd+1;
q.push(x->right);
}
q.pop();
x=q.front();
}
for(auto i=r.begin(); i!=r.end(); i++)
result.push_back(i->second);
return result;
}
//Question if BT is BST or not
class Solution
{
public:
bool isBSTutil(Node *root, int min, int max)
{
if(root==NULL)
return 1;
if(root->data < min || root->data>max){
return 0;
}
return (isBSTutil(root->left , min, root->data-1) && isBSTutil(root->right, root->data+1, max));
}
bool isBST(Node* root)
{
return(isBSTutil(root, INT_MIN, INT_MAX));
}
};
// Question Construct BST from BT
void inorder(Node *root, vector<int> &v, int *preindex )
{
if(root==NULL)
return;
inorder(root->left, v, preindex);
v[(*preindex)];
(*preindex)++;
inorder(root->right, v, preindex);
}
void binarytree(Node *root, vector<int> &v, int *preindex)
{
if(root==NULL){
return ;
}
binarytree(root, v, preindex);
root->data=v[*preindex];
(*preindex)++;
binarytree(root, v, preindex);
}
Node *binaryTreeToBST (Node *root)
{
if (root==NULL){
return NULL;
}
vector<int> v(0, 1000);
int preindex=0;
inorder(root,v, &preindex);
sort(v.begin(), v.end());
preindex=0;
binarytree(root, v, &preindex);
return root;
}
//LCA of two nodes in BST
/* Approach:
For Binary search tree, while traversing the tree from top to bottom the first node which lies in between the two numbers n1 and n2 is the LCA of the nodes, i.e. the first node n with the lowest depth which lies in between n1 and n2 (n1<=n<=n2) n1 < n2. So just recursively traverse the BST in, if node's value is greater than both n1 and n2 then our LCA lies in the left side of the node, if it's is smaller than both n1 and n2, then LCA lies on the right side. Otherwise, the root is LCA (assuming that both n1 and n2 are present in BST).*/
//question vvvv imp : Largest BST in BT
/*A Tree is BST if following is true for every node x.
The largest value in left subtree (of x) is smaller than value of x.
The smallest value in right subtree (of x) is greater than value of x.*/
struct Info{
int sz;
int max;
int min;
int ans;
bool isBST;
};
Info largestbst(Node *root){
if(root==NULL){
return{0, INT_MIN, INT_MAX, 0, true};
}
if(root->left==NULL && root->right==NULL ){
return{1, root->data, root->data, 1, true};
}
Info l, r;
l=largestbst(root->left);
r=largestbst(root->right);
Info ret;
ret.sz=l.sz+r.sz+1;
if(l.isBST && r.isBST && l.max < root->data && r.min > root->data)
{
ret.min=min(l.min, min(root->data, r.min));
ret.max=max(r.max, max(root->data, l.max));
ret.ans=ret.sz;
ret.isBST=true;
return ret;
}
ret.ans=max(l.ans, r.ans);
ret.isBST=false;
return ret;
}
int largestBst(Node *root)
{
return largestbst(root).ans;
}
// Question : Binary Tree to DLL
class Solution
{
public:
void BTODLL(Node *root, Node **head, Node**prev){
if (root==NULL)
return;
BTODLL(root->left, head, prev);
if(*prev==NULL)
{
*head=root;
}
else{
root->left=(*prev);
(*prev)->right=root;
}
(*prev)=root;
BTODLL(root->right, head, prev);
}
Node * bToDLL(Node *root)
{
Node *head=NULL;
Node *prev=NULL;
BTODLL(root, &head ,&prev);
return head;
}
};