Open In App

Iterative program to count leaf nodes in a Binary Tree

Last Updated : 26 Sep, 2024
Comments
Improve
Suggest changes
104 Likes
Like
Report

Given a Binary Tree, the task is to count leaves in it. A node is a leaf node if both left and right child nodes of it are NULL.

Example:

Input:

ex-3

Output: 3
Explanation: Three leaf nodes are 3, 4 and 5 as both of their left and right child is NULL.

Input:

ex-4

Output: 3
Explanation: Three leaf nodes are 4, 6 and 7 as both of their left and right child is NULL.

Approach:

The idea is to use level order traversal. During traversal, if we find a node whose left and right children are NULL, we increment count

Follow the steps below to solve the problem:

  • Create a queue to traverse in level order manner and a variable count to keep track of the number of leaf nodes. Start by enqueuing the root node.
  • While the queue is not empty, proceed with the traversal.
    • Dequeue the front node from the queue and check its children.
    • If both the left and right children of the current node are NULL, increment the count variable by 1, indicating that a leaf node has been found.
    • If the current node has a left child, enqueue it into the queue. Similarly, if it has a right child, enqueue that child also.
    • Repeat the process until all nodes have been processed (i.e., the queue is empty).
  • Finally, return the count of leaf nodes found during the traversal.

Below is the implementation of the above approach:

C++
// C++ code to count leaf nodes in a binary tree
// using level order traversal
#include <bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    Node *left, *right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

// Function to count the leaf nodes in a binary tree
int countLeaves(Node* root) {
  
    // If the root is null, return 0
    if (root == nullptr) {
        return 0;
    }

    // Initialize a queue for level order traversal
    queue<Node*> q;
    q.push(root);

    int count = 0;

    // Traverse the tree using level order traversal
    while (!q.empty()) {
        Node* curr = q.front();
        q.pop();

        // Check if the current node is a leaf node
        if (curr->left == nullptr && curr->right == nullptr) {
            count++;
        }

        // Enqueue left and right children if they exist
        if (curr->left != nullptr) {
            q.push(curr->left);
        }
        if (curr->right != nullptr) {
            q.push(curr->right);
        }
    }

    return count;
}

int main() {
  
    // Representation of input binary tree
    //        1
    //       / \
    //      2   3
    //     / \
    //    4   5
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);

    cout << countLeaves(root) << "\n";

    return 0;
}
Java Python C# JavaScript

Output
3

Time Complexity: O(n), where n is the number of nodes in the tree, since each node is visited exactly once.
Auxiliary space: O(n), space used for queue.

Related article:


Next Article
Article Tags :
Practice Tags :

Similar Reads