Sir/Ma’am, help me in solving the question. Q) Algorithm that accepts a Binary Tree as input and prints the number of leaf nodes to standard output.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Algorithm to count number of leaf in a Binary Tree
struct node* NewNode(int data) {
struct node* node = new(struct node); // "new" is like "malloc"
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right==NULL)
return 1;
else
return getLeafCount(node->left )+ getLeafCount(node->right);
}