top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to count the number of leaves in a binary tree without recursion?

+4 votes
503 views
How to count the number of leaves in a binary tree without recursion?
posted Jul 18, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

A node is a leaf node if both left and right child nodes of it are NULL

NumberOfLeafNodes(root);
int NumberOfLeafNodes(NODE *p)
{
    NODE *nodestack[50];
    int top=-1;
    int count=0;
    if(p==NULL)
        return 0;
    nodestack[++top]=p;
    while(top!=-1)
    {
        p=nodestack[top--];
        while(p!=NULL)
        {
            if(p->leftchild==NULL && p->rightchild==NULL)
                count++;
            if(p->rightchild!=NULL)
                nodestack[++top]=p->rightchild;
            p=p->leftchild;      
        }
    }
return count;
}
answer Jul 19, 2016 by Vrije Mani Upadhyay
...