size of a tree

Write a C program to Calculate Size of a tree
Size of a tree is the number of elements present in the tree.
Size() function recursively calculates the size of a tree.
Size of a tree = Size of left subtree + 1 + Size of right subtree

#include<stdio.h>
#include<stdlib.h>

struct node{

 int data;
 struct node *left;
 struct node *right;
};

//create new node when function CreateNode Called

struct node *CreateNode(int data)   //function to create new node & returning a node so return type is struct
{

struct node *new_node=(struct node*)malloc(sizeof(struct node));  //Allocate memory to new node

new_node->data=data;

new_node->left=NULL;
new_node->right=NULL;
return new_node;
}

int TreeSize(struct node *root)
{

if(root==NULL)
return 0;

else return (TreeSize(root->left)+1+TreeSize(root->right));
}

main()
{

struct node *root;
root=CreateNode(1);

root->left=CreateNode(2);
root->right=CreateNode(3);

root->left->left=CreateNode(4);
root->right->right=CreateNode(5);

root->left->left->right=CreateNode(10);
root->right->right->left=CreateNode(11);


int x=TreeSize(root);
printf("\nTree Size is :%d\n",x);

}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics