PROBLEM DESCRIPTION
Given a Binary Tree of size N, You have to count leaves in it.
SOLUTION
This can be solved by recursively traversing the binary tree and counting its leaf nodes. The countLeaves() function checks if the current node is null; if it is, the function returns 0, meaning there are no leaves at this point. If the node is a leaf (i.e., it has no left or right children), the function returns 1, counting this node as a leaf. Otherwise, the function recursively calls itself on both the left and right child nodes, summing up the total number of leaves found in both subtrees.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Tree
{
int countLeaves(Node node)
{
if(node == null)
return 0;
if(node.left == null && node.right == null)
return 1;
return countLeaves(node.left) + countLeaves(node.right);
}
}