Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int data;
- struct node* left;
- struct node* right;
- };
- int sum1=0,sum2=0;
- void inorder(struct node* root){
- if(root == NULL) return;
- inorder(root->left);
- printf("%d ->", root->data);
- inorder(root->right);
- }
- void preorder(struct node* root){
- if(root == NULL) return;
- printf("%d ->", root->data);
- preorder(root->left);
- preorder(root->right);
- }
- void postorder(struct node* root) {
- if(root == NULL) return;
- postorder(root->left);
- postorder(root->right);
- printf("%d ->", root->data);
- }
- struct node* createNode(value){
- struct node* newNode = malloc(sizeof(struct node));
- newNode->data = value;
- newNode->left = NULL;
- newNode->right = NULL;
- return newNode;
- }
- struct node* insertLeft(struct node *root, int value) {
- root->left = createNode(value);
- return root->left;
- }
- struct node* insertRight(struct node *root, int value){
- root->right = createNode(value);
- return root->right;
- }
- int sum11(struct node *root)
- {
- if(root==NULL) return;
- else
- {
- if(root->data%2==0) sum1+=root->data;
- sum11(root->left);
- sum11(root->right);
- }
- return (sum1);
- }
- int sum22(struct node *root)
- {
- if(root==NULL) return;
- else
- {
- if(root->data%2!=0) sum2+=root->data;
- sum22(root->left);
- sum22(root->right);
- }
- return (sum2);
- }
- void inser(struct node *root)
- {
- if(root==NULL) return;
- else
- {
- if(root->data<0) root->data=0;
- }
- printf("->%d",root->data);
- inser(root->left);
- inser(root->right);
- }
- int main(){
- struct node* root = createNode(1);
- insertLeft(root, 12);
- insertRight(root, 9);
- insertLeft(root->left, -1);
- insertRight(root->left, 6);
- printf("Inorder traversal \n");
- inorder(root);
- printf("\nPreorder traversal \n");
- preorder(root);
- printf("\nPostorder traversal \n");
- postorder(root);
- int e=sum11(root);
- int d=sum22(root);
- printf("\n%d %d\n",e,d);
- inser(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement