http://www.geeksforgeeks.org/iterative-program-count-leaf-nodes-binary-tree/
Problem solving with programming: Counting the number of leaf nodes in a binary tree
Given a binary tree, write a program to count the number of leaf nodes. A leaf node is a node which does not have left and right children.
http://www.geeksforgeeks.org/write-a-c-program-to-get-count-of-leaf-nodes-in-a-binary-tree/
Use Queue ===>we can use Queue. Idea is to use Level Order traversal.
http://k2code.blogspot.com/2015/09/program-to-count-leaf-nodes-in-binary.html
http://comproguide.blogspot.com/2014/05/counting-number-of-leaf-nodes-in-binary.html
unsigned
int
getLeafCount(
struct
Node* node)
{
// If tree is empty
if
(!node)
return
0;
// Initialize empty queue.
queue<Node *> q;
// Do level order traversal starting from root
int
count = 0;
// Initialize count of leaves
q.push(node);
while
(!q.empty())
{
struct
Node *temp = q.front();
q.pop();
if
(temp->left != NULL)
q.push(temp->left);
if
(temp->right != NULL)
q.push(temp->right);
if
(temp->left == NULL && temp->right == NULL)
count++;
}
return
count;
}
Given a binary tree, write a program to count the number of leaf nodes. A leaf node is a node which does not have left and right children.
http://www.geeksforgeeks.org/write-a-c-program-to-get-count-of-leaf-nodes-in-a-binary-tree/
unsigned
int
getLeafCount(
struct
node* node)
{
if
(node == NULL)
return
0;
if
(node->left == NULL && node->right==NULL)
return
1;
else
return
getLeafCount(node->left)+
getLeafCount(node->right);
}
http://k2code.blogspot.com/2015/09/program-to-count-leaf-nodes-in-binary.html
int
countLeaves(Node root)
{
int
count=
0
;
if
(root==
null
)
return
0
;
Queue<Node> myqueue =
new
Queue<Node>();
myqueue.push(root);
while
(!myqueue.empty())
{
Node temp;
temp=myqueue.pop();
//take the top element from the queue
if
(temp.left==
null
&& temp.right==
null
)
count++;
if
(temp.left!=
null
)
myqueue.push(temp.left);
if
(temp.right!=
null
)
myqueue.push(temp.right);
}
return
count;
}
Read full article from Problem solving with programming: Counting the number of leaf nodes in a binary treeint leafNodesIterative(TreeNode *root ){if( root == NULL )return 0;int count = 0;queue<TreeNode*> Q;Q.push( root );while ( !Q.empty() ){TreeNode * temp = Q.front();Q.pop();if( temp->left == NULL && temp->right == NULL )count++;if( temp->left != NULL )Q.push( temp->left );if( temp->right != NULL )Q.push( temp->right );}return count;}