C program to determine the number of nodes in a binary tree

Question:Write a C program to determine the number of elements(or size) in a binary tree?

Solution:

#include<stdio.h>
struct binarysearchtree
{
int data;
struct binarysearchtree* left;
struct binarysearchtree* right;
};
typedef struct binarysearchtree* tree;


int tree_size(tree T)
{
if(T==NULL)
return 0;
else
{
return 1+tree_size(T->left)+tree_size(T->right);
}
}


Click Here For More Questions

No comments:

Post a Comment