How to count the number of nodes present in a binary tree?

In, this program we count the number of nodes present in a binary tree.

 

=>how to count the number of nodes present in a binary tree.

typedef struct tree
{
int data;
struct tree *lft;
struct tree *rt;
}treenode;

int count(treenode *root);

int main()
{
int option,data;
treenode *root=NULL;

while(1)
{
menu();
printf("enter the choice");
scanf("%d",&option);
switch(option)
{
case 0:
return 0;

break;
case 1:
printf("count=%d",count(root));
break;

default:printf("not found");

}
}
return 0;
}

int count(treenode* root)
{

if(root==NULL)
return 0;

return 1 + count(root->lft) + count(root->rt);

}

Leave a Reply