Interview Questions For All

Interview Questions For All Who Are Waiting For Jobs

Thursday, July 24, 2008

C-program to count the leaves in a tree

12. Write a C program to count the number of leaves in a tree
Solution:


#include
struct binarysearchtree{
int data;
struct binarysearchtree* left;
struct binarysearchtree* right;
};
typedef struct binarysearchtree* tree;

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


piot at 7:52 AM

No comments:

Post a Comment

‹
›
Home
View web version
Powered by Blogger.