Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 ?
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 ?
Level 0: Root node (8) - not a leaf, ignore
Level 1: Nodes (8, 6) - not leaves, ignore
Level 2: Nodes (9, 7, 10) - only 9 is a leaf, sum = 9
Level 3: Nodes (2, 12, 5, 11) - all are leaves, sum = 30
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.
