Count the number of leaves in a tree

Started by Kalyan, Aug 02, 2008, 10:01 PM

Previous topic - Next topic

Kalyan

Count the number of leaves in a tree

#include<stdio.h>
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);
}