DSA Series Chapter 22 : COUNT LEAF NODES IN BINARY TREE(C Version)

 


DSA Series Chapter 22 : COUNT LEAF NODES IN BINARY TREE(C Version)

 [Follow here] for daily coding insights

1. Objective

To count the total number of leaf nodes in a Binary Tree using a recursive approach in C.


2. Definition (Exam-Ready)

A leaf node in a binary tree is a node that has no left child and no right child.


3. Example Binary Tree

            1
          /   \
         2     3
        / \
       4   5

Leaf Nodes

  • Leaf nodes: 4, 5, 3

  • Leaf Count = 3


4. Identification Rule for Leaf Node

A node is a leaf node if:

left == NULL AND right == NULL

5. Core Logic (Recursive)

  • If tree is empty → return 0

  • If current node is a leaf → return 1

  • Otherwise:

    • Count leaf nodes in left subtree

    • Count leaf nodes in right subtree


6. Recursive Formula

leafCount(node) =
    0                          if node == NULL
    1                          if node is a leaf
    leafCount(left) + leafCount(right) otherwise

7. Structure Definition

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

8. Function to Count Leaf Nodes

int countLeafNodes(struct Node* root) {
    if (root == NULL)
        return 0;

    if (root->left == NULL && root->right == NULL)
        return 1;

    return countLeafNodes(root->left) + countLeafNodes(root->right);
}

9. Line-by-Line Explanation

if (root == NULL)
    return 0;
  • Base case

  • Empty tree contributes zero leaf nodes


if (root->left == NULL && root->right == NULL)
    return 1;
  • Checks if current node is a leaf

  • If yes, count it as one leaf


return countLeafNodes(root->left) + countLeafNodes(root->right);
  • Recursively count leaf nodes from both subtrees

  • Add their results


10. Complete C Program

#include <stdio.h>
#include <stdlib.h>

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

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

int countLeafNodes(struct Node* root) {
    if (root == NULL)
        return 0;

    if (root->left == NULL && root->right == NULL)
        return 1;

    return countLeafNodes(root->left) + countLeafNodes(root->right);
}

int main() {
    struct Node* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);

    printf("Total leaf nodes: %d", countLeafNodes(root));
    return 0;
}

11. Output

Total leaf nodes: 3

12. Time and Space Complexity

Time Complexity

  • O(n)
    Every node is visited once.

Space Complexity

  • O(h) (recursive call stack)

    • Balanced tree → O(log n)

    • Skewed tree → O(n)


13. Important Exam Notes

  • Leaf node = degree 0

  • Uses DFS recursion

  • Do not confuse leaf nodes with internal nodes

  • Works for any binary tree (not BST-specific)


14. Common Beginner Mistakes

  • Counting nodes with one child as leaf

  • Forgetting base case (root == NULL)

  • Using global variables unnecessarily

  • Confusing leaf count with total node count


15. Difference: Total Nodes vs Leaf Nodes

AspectTotal NodesLeaf Nodes
Includes rootYesOnly if root is leaf
Child conditionAnyNo children
Formula1 + L + RConditional

16. Learning Outcome

After this post, learners can:

  • Identify leaf nodes correctly

  • Implement leaf counting in C

  • Answer exam questions confidently

  • Extend logic to internal node counting

ALSO CHECK 

DS(Data Structure) Python&C Edition

DSA Series Chapter 1 Time & Space Complexity ( C Edition)

DSA Series Chapter 2 Arrays  (C Version)


DSA Series Chapter 2 Arrays  (Python Version)

DSA Series Chapter 3 Strings (C Version)


DSA Series Chapter 3 Strings (Python Version)


DSA Series Chapter 4 Linked Lists (Python Version)


DSA Series Chapter 4 Linked Lists (C Version)


DSA Series Chapter 5 Stacks (Python Version)


DSA Series Chapter 5  Stacks  (C Version)




















DSA Series Chapter 13 - Binary Tree traversal, BFS, DFT/DFS, Preorder Traversal In Binary Tree


DSA Series Chapter 14 - InOrder Traversal (Binary Tree C Version)


DSA Series Chapter 14 - InOrder Traversal (Binary Tree Python Version)

 

DSA Series Chapter 15 – POSTORDER TRAVERSAL (BINARY TREE)(Python Version)


DSA Series Chapter 15 – POSTORDER TRAVERSAL (BINARY TREE)(C Version)


DSA Series Chapter 16 – LEVEL ORDER TRAVERSAL(BINARY TREE BFS)(C Version)


DSA Series Chapter 16 – LEVEL ORDER TRAVERSAL(BINARY TREE BFS)(Python Version)



DSA Series Chapter 19 : DELETION IN BINARY TREE(C Version)

DSA Series Chapter 19 : DELETION IN BINARY TREE(Python Version)


DSA Series Chapter 20 – HEIGHT OF BINARY TREE(C Version)

DSA Series Chapter 20 – HEIGHT OF BINARY TREE(Python Version)






Tail Recursion Optimization: How Compilers Convert Recursion Into Iteration


Advanced Recursion: Memoization vs. Tabulation — The Deep Optimization Blueprint for Professionals


Advanced Sorting Algorithms: Merge Sort Internals — Merge Tree, Cache Behavior & Real Cost Analysis


Enjoyed this post? [Follow here] for daily coding insights

Comments