#include<stdio.h>
#include<stdlib.h>
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;
}
struct node* insertnode(struct node* root,int num)
{
if(root==NULL)
{
root=createnode(num);
return root;
}
if(num<root->data)
root->left=insertnode(root->left,num);
else
root->right=insertnode(root->right,num);
return root;
}
void printPreorder(struct node* root)
{
if(root==NULL)
return;
printPreorder(root->left);
printPreorder(root->right);
}
void printInorder(struct node* root)
{
if(root==NULL)
return;
printInorder(root->left);
printInorder(root->right);
}
void printPostorder(struct node* root)
{
if(root==NULL)
return;
printPostorder(root->left);
printPostorder(root->right);
}
int main() {
struct node* root=createnode(20);
root->left=createnode(15);
root->right=createnode(25);
root->left->left=createnode(13);
root->left->right=createnode(17);
root->left->left->left=createnode(9);
root->right->left=createnode(22);
root->right->right=createnode(26);
root->right->right->right=createnode(30);
printPreorder(root);
printInorder(root);
printPostorder(root);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CnN0cnVjdCBub2RlewoJICAgICAgICAgIGludCBkYXRhOwoJICAgICAgICAgIHN0cnVjdCBub2RlKiBsZWZ0OwoJICAgICAgICAgIHN0cnVjdCBub2RlKiByaWdodDsKfTsKCnN0cnVjdCBub2RlKiBjcmVhdGVub2RlKGludCB2YWwpCnsKCXN0cnVjdCBub2RlKiBwPShzdHJ1Y3Qgbm9kZSopbWFsbG9jKHNpemVvZihzdHJ1Y3Qgbm9kZSkpOwoJcC0+ZGF0YT12YWw7CglwLT5sZWZ0PU5VTEw7CglwLT5yaWdodD1OVUxMOwoJcmV0dXJuIHA7Cn0Kc3RydWN0IG5vZGUqIGluc2VydG5vZGUoc3RydWN0IG5vZGUqIHJvb3QsaW50IG51bSkKewoJaWYocm9vdD09TlVMTCkKCXsKCQlyb290PWNyZWF0ZW5vZGUobnVtKTsKCQlyZXR1cm4gcm9vdD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