#include <bits/stdc++.h>
using namespace std;
struct node
{
int value;
struct node* left;
struct node* right;
};
struct node* creation(int x)
{
struct node* temp = (struct node*) malloc(sizeof(struct node));
temp->value = x;
temp->right = NULL;
temp->left = NULL;
return temp;
};
void insertion(struct node** root, int x)
{
if(*root==NULL) {*root = creation(x); return;}
else
{
if (x <= (*root)->value) insertion(&((*root)->left),x);
else insertion(&((*root)->right),x);
}
}
void print(struct node *root)
{
if (root == NULL) return;
queue<node*> q;
q.push(root);
while(!q.empty())
{
cout << (q.front()->value) << " ";
if ((q.front())->left != NULL) q.push((q.front())->left);
if ((q.front())->right != NULL) q.push((q.front())->right);
q.pop();
}
}
void search(struct node *root, int level, int x)
{
if (root == NULL) {cout << "NOT found" << endl;return;}
else
{
if (root -> value == x) {cout << "found at level: " << level << endl; return;}
else if (root -> value < x){search(root->right, level + 1,x);}
else if (root -> value > x){search(root->left, level + 1,x);}
}
}
int height (struct node* root, int h)
{
if (root == NULL) return h;
else {return max(height(root->right, h+1),height(root->left, h+1));}
}
int maxi (struct node* root)
{
if (root == NULL) return -1e9;
if (root-> right == NULL) return root->value;
return maxi(root->right);
}
int main()
{
struct node* root = creation(5);
insertion(&root, 10);
insertion(&root, 3);
insertion(&root, 4);
insertion(&root, 6);
cout << maxi(root) << endl;
print(root);
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IG5vZGUKewogICAgaW50IHZhbHVlOwogICAgc3RydWN0IG5vZGUqIGxlZnQ7CiAgICBzdHJ1Y3Qgbm9kZSogcmlnaHQ7Cn07CgpzdHJ1Y3Qgbm9kZSogY3JlYXRpb24oaW50IHgpCnsKICAgIHN0cnVjdCBub2RlKiB0ZW1wID0gKHN0cnVjdCBub2RlKikgbWFsbG9jKHNpemVvZihzdHJ1Y3Qgbm9kZSkpOwogICAgdGVtcC0+dmFsdWUgPSB4OwogICAgdGVtcC0+cmlnaHQgPSBOVUxMOwogICAgdG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