Print the number of leaf nodes in a binary tree

In this chapter we shall print the number of leaf nodes in a binary tree.

Problem Statement:

You are given the root node of the tree. You need to return the total number of leaf nodes present in that tree.

Example:

Consider the below tree

print_the_total_count_of_leaf_nodes

In the above tree, there are 4 leaf nodes.

d, e, f, g

You need to write a program to print the same.

So from the image above, we can see that, the nodes whose left child is null and right child is also null, such node is called as a leaf node.

So we can write a recursive function to check all the nodes in a tree, if it is a leaf node or not.

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *