problem no 53:(problem on binary tree): counting the number of the leaf nodes in the binary tree.
Question:
Given a Binary Tree of size N , You have to count leaves in it. For example, there are two leaves in following tree
1
/ \
10 39
/
5
Example 1:
Input:
Given Tree is
4
/ \
8 10
/ / \
7 5 1
/
3
Output:
3
Explanation:
Three leaves are 3 , 5 and 1.
ANSWER:
int countLeaves(Node* root)
{
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
else
return countLeaves(root->left)+
countLeaves(root->right);
}
Comments
Post a Comment