Count Leaves in Binary Tree
Idea is to check if both left and right nodes are null if so increment value
Code:
int countLeaves(Node node)
{
int count=0;
if(node==null)
return 0;
if(node.left==null && node.right==null)
return 1;
else
{
count+=countLeaves(node.left);
count+=countLeaves(node.right);
}
return count;
}
0 Comments:
Post a Comment