Find multiplication of sums of data of leaves at same levels in Python

Binary trees often require level-by-level processing to solve complex problems. In this problem, we need to find the sum of leaf nodes at each level and multiply all these sums together.

Problem Statement

Given a binary tree, we need to perform the following operations ?

  • For each level, find sum of all leaves if there are leaves at this level. Otherwise ignore it.

  • Find multiplication of all sums and return it.

For example, if we have the following tree structure ?

8 8 6 9 7 10 2 12 5 11 Leaf Node Internal Node

The output will be 270. Level 2 has one leaf node (9), and level 3 has four leaf nodes (2, 12, 5, 11). So the result is 9 × (2 + 12 + 5 + 11) = 270.

Algorithm

We'll use a level-order traversal (BFS) approach to process each level separately ?

  • Use a queue to traverse the tree level by level

  • For each level, identify leaf nodes and sum their values

  • Multiply all level sums to get the final result

Implementation

from collections import deque

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

def is_leaf(node):
    return node.left is None and node.right is None

def find_multiplication_of_leaf_sums(root):
    if not root:
        return 0
    
    result = 1
    queue = deque([root])
    
    while queue:
        level_size = len(queue)
        level_sum = 0
        has_leaf = False
        
        # Process all nodes at current level
        for _ in range(level_size):
            current = queue.popleft()
            
            # Check if current node is a leaf
            if is_leaf(current):
                has_leaf = True
                level_sum += current.data
            
            # Add children to queue for next level
            if current.left:
                queue.append(current.left)
            if current.right:
                queue.append(current.right)
        
        # Multiply result with level sum if leaves were found
        if has_leaf:
            result *= level_sum
    
    return result

# Create the tree from the example
root = TreeNode(8)
root.left = TreeNode(8)
root.right = TreeNode(6)
root.left.left = TreeNode(9)
root.left.right = TreeNode(7)
root.left.right.left = TreeNode(2)
root.left.right.right = TreeNode(12)
root.right.right = TreeNode(10)
root.right.right.left = TreeNode(5)
root.right.right.right = TreeNode(11)

result = find_multiplication_of_leaf_sums(root)
print(f"Multiplication of leaf sums: {result}")
Multiplication of leaf sums: 270

How It Works

The algorithm processes each level of the tree separately ?

  1. Level 0: Root node (8) - not a leaf, ignore

  2. Level 1: Nodes (8, 6) - not leaves, ignore

  3. Level 2: Nodes (9, 7, 10) - only 9 is a leaf, sum = 9

  4. Level 3: Nodes (2, 12, 5, 11) - all are leaves, sum = 30

  5. Final result: 9 × 30 = 270

Time and Space Complexity

  • Time Complexity: O(n), where n is the number of nodes in the tree

  • Space Complexity: O(w), where w is the maximum width of the tree

Conclusion

This solution efficiently finds the multiplication of leaf node sums at each level using BFS traversal. The key insight is to process nodes level by level and only consider leaf nodes for the sum calculation.

Updated on: 2026-03-25T09:23:13+05:30

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements