Write a definition of the function leavesCount that takes as

Write a definition of the function leavesCount that takes as a parameter a pointer to the root node of a binary tree and returns the number os leaves in a binary tree. Add this function to the class binaryTreeType an create a program to test this function.

Solution

============================================================
----------
Answer:
----------

//Method to count the number of leaves in a binary tree
int binaryTreeType::countLeaves(node *root)
{
   //If root is NULL return 0
   if(root ==NULL)
   {

       return0;
   }
   //If root of left and right are NULL, indicates a leaf node and return 1 leaf node found
   else if(root -> left == NULL && root-> right == NULL)
   {

       return 1;
   }
   else
   {
       //Calling recursively to find the leaves nodes if left sub tree not NULL or right sub tree not NULL

       return countLeaves(root->left) + countLeaves(root->right);
   }
}

============================================================

Write a definition of the function leavesCount that takes as a parameter a pointer to the root node of a binary tree and returns the number os leaves in a binar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site