#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* createnode(int val)
{
struct node* p=(struct node*)malloc(sizeof(struct node));
p->data=val;
p->left=NULL;
p->right=NULL;
return p;
}
int max(int a,int b,int c)
{
if(a>b && a>c)
return a;
if(b>a && b>c)
return b;
else
return c;
}
int maxnode(struct node* root)
{
if(root==NULL)
return -1;
int lmax=maxnode(root->left);
int rmax=maxnode(root->right);
int res=max(root->data,lmax,rmax);
return res;
}
void preorder(struct node* root)
{
if (root==NULL)
return;
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(struct node* root)
{
if (root==NULL)
return;
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
void postorder(struct node* root)
{
if (root==NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
int main() {
struct node* root=createnode(1);
root->left=createnode(2);
root->left->left=createnode(4);
root->left->right=createnode(5);
root->right=createnode(8);
root->right->left=createnode(6);
root->right->right=createnode(9);
int node= maxnode(root);
printf("Maximum node is=%d \n",node);
preorder(root);
printf("\n");
inorder(root);
printf("\n");
postorder(root);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IG5vZGV7CglpbnQgZGF0YTsKCXN0cnVjdCBub2RlKiBsZWZ0OwoJc3RydWN0IG5vZGUqIHJpZ2h0Owp9OwpzdHJ1Y3Qgbm9kZSogY3JlYXRlbm9kZShpbnQgdmFsKQp7CglzdHJ1Y3Qgbm9kZSogcD0oc3RydWN0IG5vZGUqKW1hbGxvYyhzaXplb2Yoc3RydWN0IG5vZGUpKTsKCXAtPmRhdGE9dmFsOwoJcC0+bGVmdD1OVUxMOwoJcC0+cmlnaHQ9TlVMTDsKCXJldHVybiBwOwp9CmludCBtYXgoaW50IGEsaW50IGIsaW50IGMpCnsKCWlmKGE+YiAmJiBhPmMpCglyZXR1cm4gYTsKCWlmKGI+YSAmJiB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