#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node * getNewNode(int data)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->leftChild = NULL;
newNode->rightChild = NULL;
return newNode;
}
struct node * insert(struct node * root, int data)
{
if (root == NULL)
{
root = getNewNode(data);
}
else if (data <= root->data)
{
root->leftChild = insert(root->leftChild, data);
}
else
{
root->rightChild = insert(root->rightChild, data);
}
return root;
}
int countLeaves(struct node * root){
if (root == NULL)
return 0;
else{
if(root->leftChild == NULL && root->rightChild==NULL)
return 1;
else {
return countLeaves(root->leftChild) + countLeaves(root->rightChild);
}
}
}
int main()
{
struct node *root = NULL;
root = getNewNode(50);
root->leftChild = getNewNode(40);
root->rightChild = getNewNode(60);
root->leftChild->leftChild = getNewNode(30);
root->leftChild->rightChild = getNewNode(45);
root->rightChild->rightChild = getNewNode(70);
root->leftChild->rightChild->leftChild = getNewNode(42);
printf("Number of leaves: %d ",countLeaves(root));
return 0;
}
==================================================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and...
WİTH C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 125 pointsl Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, rightChild). Make content random. 60 50 40 60 70 30 45 42 b) 125 points Display the trees on screen the way we did in slide 9 using listAll...