-
Notifications
You must be signed in to change notification settings - Fork 3
/
lecture26.cpp
323 lines (295 loc) · 8.02 KB
/
lecture26.cpp
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
#include<iostream>
using namespace std;
// 543. Diameter of Binary Tree
// class Solution {
// public:
// int ans=0;
// int diameterOfBinaryTree2(TreeNode* root){
// if(root==nullptr){
// return -1;
// }
// int left= diameterOfBinaryTree2(root->left);
// int right= diameterOfBinaryTree2(root->right);
// ans=max(ans,left+right+2);
// return max(left,right)+1;
// }
// int diameterOfBinaryTree(TreeNode* root) {
// if(root==nullptr){
// return 0;
// }
// diameterOfBinaryTree2(root);
// return ans;
// }
// };
// 145. Binary Tree Postorder Traversal
// class Solution {
// public:
// void postorderTraversal(TreeNode* root,vector<int>&ans){
// if(root==nullptr){
// return;
// }
// postorderTraversal(root->left,ans);
// postorderTraversal(root->right,ans);
// ans.push_back(root->val);
// }
// vector<int> postorderTraversal(TreeNode* root) {
// vector<int>ans;
// postorderTraversal(root,ans);
// return ans;
// }
// };
// 94. Binary Tree Inorder Traversal
// class Solution {
// public:
// void inorderTraversal(TreeNode* root,vector<int>&ans){
// if(root==nullptr){
// return;
// }
// inorderTraversal(root->left,ans);
// ans.push_back(root->val);
// inorderTraversal(root->right,ans);
// }
// vector<int> inorderTraversal(TreeNode* root) {
// vector<int>ans;
// inorderTraversal(root,ans);
// return ans;
// }
// };
// 144. Binary Tree Preorder Traversal
// class Solution {
// public:
// void preorderTraversal(TreeNode* root,vector<int>&ans){
// if(root==nullptr){
// return;
// }
// ans.push_back(root->val);
// preorderTraversal(root->left,ans);
// preorderTraversal(root->right,ans);
// }
// vector<int> preorderTraversal(TreeNode* root) {
// vector<int>ans;
// preorderTraversal(root,ans);
// return ans;
// }
// };
// 111. Minimum Depth of Binary Tree
// class Solution {
// public:
// int minDepth2(TreeNode* root){
// if(root==nullptr){
// return INT_MAX;
// }
// if(root->left==nullptr&&root->right==nullptr){
// return 1;
// }
// return min(minDepth2(root->left),minDepth2(root->right))+1;
// }
// int minDepth(TreeNode* root) {
// if(root==nullptr){
// return 0;
// }
// return minDepth2(root);
// }
// };
// Maximum Depth of Binary Tree
// class Solution {
// public:
// int maxDepth(TreeNode* root) {
// if(root==nullptr){
// return 0;
// }
// int left= maxDepth(root->left);
// int right= maxDepth(root->right);
// return max(left,right)+1;
// }
// };
// 450. Delete Node in a BST
// class Solution {
// public:
// int minimum(TreeNode* root){
// if(root->left==nullptr){
// return root->val;
// }
// return minimum(root->left);
// }
// TreeNode* deleteNode(TreeNode* root, int key) {
// if(root==nullptr){
// return nullptr;
// }
// if(root->val==key){
// if(root->left==nullptr&&root->right==nullptr){
// return nullptr;
// }
// else if(root->left!=nullptr&&root->right!=nullptr){
// int minV= minimum(root->right);
// root->val=minV;
// root->right=deleteNode(root->right,minV);
// }
// else if(root->left!=nullptr){
// return root->left;
// }
// else{
// return root->right;
// }
// }
// else if(root->val<key){
// root->right=deleteNode(root->right,key);
// }
// else{
// root->left=deleteNode(root->left,key);
// }
// return root;
// }
// };
// 236. Lowest Common Ancestor of a Binary Tree
// class Solution {
// public:
// vector<TreeNode*> path(TreeNode* root, TreeNode* p){
// if(root==nullptr){
// vector<TreeNode*>baseCase;
// return baseCase;
// }
// if(root==p){
// vector<TreeNode*>baseCase;
// baseCase.push_back(p);
// return baseCase;
// }
// vector<TreeNode*> left= path(root->left,p);
// vector<TreeNode*> right= path(root->right,p);
// if(left.size()!=0){
// left.push_back(root);
// return left;
// }
// else if(right.size()!=0){
// right.push_back(root);
// return right;
// }
// else{
// return left;
// }
// }
// TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// TreeNode* finalAns= nullptr;
// vector<TreeNode*>p1= path(root,p);
// vector<TreeNode*>p2= path(root,q);
// int id1=p1.size()-1;
// int id2=p2.size()-1;
// while(id1>=0&&id2>=0&&p1[id1]==p2[id2]){
// finalAns= p1[id1];
// id1--;
// id2--;
// }
// return finalAns;
// }
// };
// 235. Lowest Common Ancestor of a Binary Search Tree
// class Solution {
// public:
// TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// while(root!=nullptr){
// if(p->val>root->val&&q->val>root->val){
// root=root->right;
// }
// else if(p->val<root->val&&q->val<root->val){
// root=root->left;
// }
// else{
// break;
// }
// }
// return root;
// }
// };
// 669. Trim a Binary Search Tree
// class Solution {
// public:
// TreeNode* trimBST(TreeNode* root, int low, int high) {
// if(root==nullptr){
// return nullptr;
// }
// if(root->val<low){
// return trimBST(root->right,low,high);
// }
// else if(root->val>high){
// return trimBST(root->left,low,high);
// }
// else{
// root->left=trimBST(root->left,low,high);
// root->right=trimBST(root->right,low,high);
// }
// return root;
// }
// };
class binaryTree{
public:
class Node{
public:
int val;
Node *left,*right;
Node(int val){
this->val=val;
left=nullptr;
right=nullptr;
}
};
private:
Node *root=nullptr;
Node *insert(Node *node,int value,string dirs,int idx){
if(node==nullptr){
Node *base= new Node(value);
return base;
}
if(idx==dirs.length()){
node->val=value;
return node;
}
if(dirs[idx]=='R'){
node->right=insert(node->right,value,dirs,idx+1);
}
else{
node->left=insert(node->left,value,dirs,idx+1);
}
return node;
}
void display(Node *node){
if(node==nullptr){
return;
}
if(node->left!=nullptr){
cout<<node->left->val<<" -> ";
}
else{
cout<<" -> ";
}
cout<<node->val<<" ";
if(node->right!=nullptr){
cout<<" <- "<<node->right->val;
}
else{
cout<<" <- ";
}
cout<<"\n";
display(node->left);
display(node->right);
}
public:
void insert(int value,string dirs){
this->root=insert(root,value,dirs,0);
}
public:
void display(){
display(root);
}
};
int main(){
binaryTree bt1;
bt1.insert(10,"");
bt1.insert(15,"R");
bt1.insert(25,"L");
bt1.insert(18,"LR");
bt1.insert(51,"R");
bt1.insert(16,"LL");
bt1.insert(21,"RL");
bt1.display();
return 0;
}