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
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.