Count the Number of Leaf Nodes in a Binary Tree

problem-solving
Published

December 13, 2024

Binary trees are fundamental data structures in computer science, used extensively in various applications. Often, we need to perform specific operations on these trees, such as counting the number of leaf nodes. Leaf nodes are nodes without any children – the endpoints of branches in the tree. This blog post will look at different approaches to efficiently count leaf nodes in a binary tree using Python.

Understanding Binary Trees

Before diving into the code, let’s briefly recap the structure of a binary tree. Each node in a binary tree can have at most two children, referred to as the left child and the right child. A node with no children is a leaf node. The topmost node is called the root.

Method 1: Recursive Approach

Recursion is a natural fit for traversing tree structures. This approach involves a function that calls itself to process the left and right subtrees. The base case is when we reach a leaf node (a node with no children).

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

def count_leaf_nodes_recursive(root):
    """
    Counts leaf nodes in a binary tree using recursion.

    Args:
      root: The root node of the binary tree.

    Returns:
      The number of leaf nodes in the tree.  Returns 0 if the tree is empty.
    """
    if root is None:
        return 0
    if root.left is None and root.right is None:
        return 1
    else:
        return count_leaf_nodes_recursive(root.left) + count_leaf_nodes_recursive(root.right)

# Example Usage
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

leaf_count = count_leaf_nodes_recursive(root)
print(f"Number of leaf nodes (recursive): {leaf_count}")  # Output: 3

This recursive function elegantly handles the base case (empty tree or leaf node) and recursively processes the subtrees.

Choosing the Right Method

Both recursive and iterative approaches are valid for counting leaf nodes. The choice often depends on personal preference, coding style, and the specific characteristics of the tree being processed. Recursive solutions are often considered more concise and easier to understand for smaller trees, while iterative approaches using BFS might be preferred for very large trees to avoid potential stack overflow issues.