Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- typedef struct node{
- int data;
- struct node* left;
- struct node* right;
- }nodeT;
- nodeT* createnode(int data){
- nodeT* newnode = (nodeT*)malloc(sizeof(nodeT));
- newnode->data = data;
- newnode->left = NULL;
- newnode->right = NULL;
- return newnode;
- }
- nodeT* insertnode(nodeT* root,int data)
- {
- // nodeT* newnode = createnode
- if(root == NULL)return createnode(data);
- if(data < root->data){
- root->left = insertnode(root->left,data);
- }
- else if(data >= root->data){
- root->right = insertnode(root->right,data);
- }
- return root;
- }
- void postorder(nodeT* root){
- if(root == NULL)return;
- postorder(root->left);
- postorder(root->right);
- printf("%d ",root->data);
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- nodeT* root = NULL;
- for(int i=0;i<n;i++){
- int x;
- scanf("%d",&x);
- root = insertnode(root,x);
- }
- postorder(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment