# Find maximum level product in Binary Tree

Given a Binary Tree having positive and negative nodes, the task is to find maximum product level in it.

Examples:

```Input :               4
/   \
2    -5
/ \    /\
-1   3 -2  6
Output: 36
Explanation :
Product of all nodes of 0'th level is 4
Product of all nodes of 1'th level is -10
Product of all nodes of 0'th level is 36
Hence maximum product is 6

Input :          1
/   \
2     3
/ \     \
4   5     8
/  \
6    7
Output :  160
Explanation :
Product of all nodes of 0'th level is 1
Product of all nodes of 1'th level is 6
Product of all nodes of 0'th level is 160
Product of all nodes of 0'th level is 42
Hence maximum product is 160
```

Prerequisites: Maximum Width of a Binary Tree

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Approach : The idea is to do level order traversal of tree. While doing traversal, process nodes of different level separately. For every level being processed, compute product of nodes in the level and keep track of maximum product.

## C++

 `// A queue based C++ program to find maximum product ` `// of a level in Binary Tree ` `#include ` `using` `namespace` `std; ` ` `  `/* A binary tree node has data, pointer to left child ` `and a pointer to right child */` `struct` `Node { ` `    ``int` `data; ` `    ``struct` `Node *left, *right; ` `}; ` ` `  `// Function to find the maximum product of a level in tree ` `// using level order traversal ` `int` `maxLevelProduct(``struct` `Node* root) ` `{ ` `    ``// Base case ` `    ``if` `(root == NULL) ` `        ``return` `0; ` ` `  `    ``// Initialize result ` `    ``int` `result = root->data; ` ` `  `    ``// Do Level order traversal keeping track of number ` `    ``// of nodes at every level. ` `    ``queue q; ` `    ``q.push(root); ` `    ``while` `(!q.empty()) { ` ` `  `        ``// Get the size of queue when the level order ` `        ``// traversal for one level finishes ` `        ``int` `count = q.size(); ` ` `  `        ``// Iterate for all the nodes in the queue currently ` `        ``int` `product = 1; ` `        ``while` `(count--) { ` ` `  `            ``// Dequeue an node from queue ` `            ``Node* temp = q.front(); ` `            ``q.pop(); ` ` `  `            ``// Multiply this node's value to current product. ` `            ``product = product * temp->data; ` ` `  `            ``// Enqueue left and right children of ` `            ``// dequeued node ` `            ``if` `(temp->left != NULL) ` `                ``q.push(temp->left); ` `            ``if` `(temp->right != NULL) ` `                ``q.push(temp->right); ` `        ``} ` ` `  `        ``// Update the maximum node count value ` `        ``result = max(product, result); ` `    ``} ` ` `  `    ``return` `result; ` `} ` ` `  `/* Helper function that allocates a new node with the ` `given data and NULL left and right pointers. */` `struct` `Node* newNode(``int` `data) ` `{ ` `    ``struct` `Node* node = ``new` `Node; ` `    ``node->data = data; ` `    ``node->left = node->right = NULL; ` `    ``return` `(node); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``struct` `Node* root = newNode(1); ` `    ``root->left = newNode(2); ` `    ``root->right = newNode(3); ` `    ``root->left->left = newNode(4); ` `    ``root->left->right = newNode(5); ` `    ``root->right->right = newNode(8); ` `    ``root->right->right->left = newNode(6); ` `    ``root->right->right->right = newNode(7); ` ` `  `    ``/* Constructed Binary tree is: ` `             ``1 ` `            ``/ \ ` `           ``2   3 ` `          ``/ \   \ ` `         ``4   5   8 ` `                ``/ \ ` `               ``6   7 */` `    ``cout << ``"Maximum level product is "` `         ``<< maxLevelProduct(root) << endl; ` `    ``return` `0; ` `} `

## Python3

 `# Python3 program to find maximum product ` `# of a level in Binary Tree ` ` `  `# Helper function that allocates a new  ` `# node with the given data and None left  ` `# and right poers.                                    ` `class` `newNode:  ` ` `  `    ``# Construct to create a new node  ` `    ``def` `__init__(``self``, key):  ` `        ``self``.data ``=` `key ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None` ` `  `# Function to find the maximum product  ` `# of a level in tree using level order ` `# traversal ` `def` `maxLevelProduct(root): ` ` `  `    ``# Base case ` `    ``if` `(root ``=``=` `None``): ` `        ``return` `0` ` `  `    ``# Initialize result ` `    ``result ``=` `root.data ` ` `  `    ``# Do Level order traversal keeping track  ` `    ``# of number of nodes at every level. ` `    ``q ``=` `[] ` `    ``q.append(root) ` `    ``while` `(``len``(q)):  ` ` `  `        ``# Get the size of queue when the level  ` `        ``# order traversal for one level finishes ` `        ``count ``=` `len``(q) ` ` `  `        ``# Iterate for all the nodes in  ` `        ``# the queue currently ` `        ``product ``=` `1` `        ``while` `(count): ` `            ``count ``-``=` `1` `             `  `            ``# Dequeue an node from queue ` `            ``temp ``=` `q[``0``] ` `            ``q.pop(``0``) ` ` `  `            ``# Multiply this node's value to  ` `            ``# current product. ` `            ``product ``=` `product ``*` `temp.data ` ` `  `            ``# Enqueue left and right children  ` `            ``# of dequeued node ` `            ``if` `(temp.left !``=` `None``): ` `                ``q.append(temp.left) ` `            ``if` `(temp.right !``=` `None``): ` `                ``q.append(temp.right) ` `         `  `        ``# Update the maximum node count value ` `        ``result ``=` `max``(product, result) ` `     `  `    ``return` `result ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``"""  ` `    ``Let us create Binary Tree  ` `    ``shown in above example """` `    ``root ``=` `newNode(``1``) ` `    ``root.left ``=` `newNode(``2``) ` `    ``root.right ``=` `newNode(``3``) ` `    ``root.left.left ``=` `newNode(``4``) ` `    ``root.left.right ``=` `newNode(``5``) ` `    ``root.right.right ``=` `newNode(``8``) ` `    ``root.right.right.left ``=` `newNode(``6``) ` `    ``root.right.right.right ``=` `newNode(``7``) ` `     `  `    ``""" Constructed Binary tree is: ` `            ``1 ` `            ``/ \ ` `        ``2 3 ` `        ``/ \ \ ` `        ``4 5 8 ` `                ``/ \ ` `            ``6 7 """` ` `  `    ``print``(``"Maximum level product is"``, ` `               ``maxLevelProduct(root)) ` ` `  `# This code is contributed by ` `# Shubham Singh(SHUBHAMSINGH10) `

Output :

`Maximum level product is 160`

Time Complexity : O(n)
Auxiliary Space : O(n)

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.