Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

prb 1:

if(root->left==nullptr && root->right==nullptr) return 1;


if(root->left==nullptr) return height(root->right)+1;
if(root->right==nullptr) return height(root->left)+1;
return max(height(root->right),height(root->left))+1;

prb 2:
auto id= new Node(data);
if(root==NULL){
return id;
}
auto st=root;
while(1){
if(root->data<data){
if(root->right==NULL){
root->right=id;
break;
}
root=root->right;

}
else{
// cout<<root->data<<' ';
if(root->left==NULL){
root->left=id;
break;
}
root=root->left;

}
}
root=st;
return root;

prb 3:
auto id= new Node(data);
if(root==NULL){
return id;
}
auto st=root;
while(1){
if(root->data<data){
if(root->right==NULL){
root->right=id;
break;
}
root=root->right;

}
else{
// cout<<root->data<<' ';
if(root->left==NULL){
root->left=id;
break;
}
root=root->left;
}
}
root=st;
return root;

prb 4:
queue<Node *> ds;
ds.push(root);
while(!ds.empty()){
Node *id=ds.front();
cout<<id->data<<' ';
ds.pop();
if(id->left!=NULL){
ds.push(id->left);
}
if(id->right!=NULL){
ds.push(id->right);
}
}

prb 5:
Node *p=NULL;
while(root!=NULL){
if(root->left==NULL){
cout<<root->data<<' ';
root=root->right;
}
else{
p=root->left;
while(p->right!=NULL && p->right!=root){
p=p->right;
}
if(p->right==NULL){
p->right=root;
root=root->left;
}
else{
p->right=NULL;
cout<<root->data<<' ';
root=root->right;
}
}
}

prb 6:
if(root->left!=NULL){
inOrder(root->left);
}
cout<<root->data<<' ';
if(root->right!=NULL){
inOrder(root->right);
}

prb 7:
stack<Node *> ds;
vector<int> ans;
ds.push(root);
while(!ds.empty()){
Node *u=ds.top();
ds.pop();
ans.push_back(u->data);
if(u->left!=NULL){
ds.push(u->left);
}
if(u->right!=NULL){
ds.push(u->right);
}

}
reverse(ans.begin(),ans.end());
for(int v: ans) cout<<v<<' ';

prb 8:
if(root==NULL) return;
postOrder(root->left);
postOrder(root->right);
cout<<root->data<<' ';

prb 9:
queue<Node *> ds;
Node *D[2000'06];
int *H[2000'06];
Node *a;
Node *b;
H[root->data]=0;
D[root->data]=root;
ds.push(root);
while(!ds.empty()){
auto u=ds.front();
if(u==NULL) continue;
if(u->data==v1) a=u;
if(u->data==v2) b=u;
ds.pop();
if(u->left!=NULL){
ds.push(u->left);
D[u->left->data]=u;
H[u->left->data]=H[u->data]+1;
}
if(u->right!=NULL){
ds.push(u->right);
D[u->right->data]=u;
H[u->right->data]=H[u->data]+1;
}
}
if(H[a->data]<H[b->data]){
swap(a,b);
}
while(H[a->data]>H[b->data]){
a=D[a->data];
}
while(a!=b){
a=D[a->data];
b=D[b->data];
}
return a;
prb 10:
#include <bits/stdc++.h>
using namespace std;
const int N=1e6;
int D[N*2+1];
vector<int> ans;
class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};

class Solution {
public:
Node* insert(Node* root, int data) {
if(root == NULL) {
return new Node(data);
} else {
Node* cur;
if(data <= root->data) {
cur = insert(root->left, data);
root->left = cur;
} else if(data>root->data) {
cur = insert(root->right, data);
root->right = cur;
}

return root;
}
}

/* you only have to complete the function given below.


Node is defined as

class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};

*/

void postOrder(int id,Node *root) {


queue< pair<int,Node *> > ds;
ds.push({id,root});
while(!ds.empty()){
pair<int,Node *> u=ds.front();
ds.pop();
if(!D[u.first]) D[u.first]=u.second->data;
if(u.second->right!=NULL) ds.push({u.first+1,u.second->right});
if(u.second->left!=NULL) ds.push({u.first-1,u.second->left});
}
}

}; //End of Solution

int main() {
ios::sync_with_stdio(0);cin.tie(0);
//freopen("test.inp","r",stdin);
Solution myTree;
Node* root = NULL;

int t;
int data;

std::cin >> t;

while(t-- > 0) {
std::cin >> data;
root = myTree.insert(root, data);
}

myTree.postOrder(1000'000,root);
for(int i=0; i<=2000'000; i++){
if(D[i]) ans.push_back(D[i]);
}
sort(ans.begin(),ans.end());
for(int v: ans) cout<<v<<' ';
return 0;
}

You might also like