algorithm problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I was trying to find an algorithm to count the number of leaf nodes in a
binary tree. I tried and could not find a nice way to do that. Can someone
give me a clue of that ?

Thanks in advance!
 
struct TreeNode
{
TreeNode *left;
TreeNode *right;
int data;
};

int count(TreeNode *root)
{
if(NULL==root)
return 0;
if(root->left ==NULL && root->right==NULL)
return 1;
return count(root->left) + count(root->right);

}
 
Back
Top