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 2'th level is 36
Hence maximum product is 36
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 2'th level is 160
Product of all nodes of 3'th level is 42
Hence maximum product is 160
Prerequisites: Maximum Width of a Binary Tree
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
int maxLevelProduct( struct Node* root)
{
if (root == NULL)
return 0;
int result = root->data;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int count = q.size();
int product = 1;
while (count--) {
Node* temp = q.front();
q.pop();
product = product * temp->data;
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
}
result = max(product, result);
}
return result;
}
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
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);
cout << "Maximum level product is "
<< maxLevelProduct(root) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node left, right;
};
static int maxLevelProduct(Node root)
{
if (root == null )
return 0 ;
int result = root.data;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (q.size() > 0 )
{
int count = q.size();
int product = 1 ;
while (count--> 0 )
{
Node temp = q.peek();
q.remove();
product = product* temp.data;
if (temp.left != null )
q.add(temp.left);
if (temp.right != null )
q.add(temp.right);
}
result = Math.max(product, result);
}
return result;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
public static void main(String args[])
{
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 );
System.out.print( "Maximum level product is " +
maxLevelProduct(root) );
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def maxLevelProduct(root):
if (root = = None ):
return 0
result = root.data
q = []
q.append(root)
while ( len (q)):
count = len (q)
product = 1
while (count):
count - = 1
temp = q[ 0 ]
q.pop( 0 )
product = product * temp.data
if (temp.left ! = None ):
q.append(temp.left)
if (temp.right ! = None ):
q.append(temp.right)
result = max (product, result)
return result
if __name__ = = '__main__' :
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 )
print ( "Maximum level product is" ,
maxLevelProduct(root))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class Node
{
public int data;
public Node left, right;
};
static int maxLevelProduct(Node root)
{
if (root == null )
{
return 0;
}
int result = root.data;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count > 0)
{
int count = q.Count;
int product = 1;
while (count-- > 0)
{
Node temp = q.Peek();
q.Dequeue();
product = product * temp.data;
if (temp.left != null )
{
q.Enqueue(temp.left);
}
if (temp.right != null )
{
q.Enqueue(temp.right);
}
}
result = Math.Max(product, result);
}
return result;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
public static void Main(String[] args)
{
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);
Console.Write( "Maximum level product is " +
maxLevelProduct(root));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .left = null ;
this .right = null ;
this .data = 0;
}
};
function maxLevelProduct(root)
{
if (root == null )
{
return 0;
}
var result = root.data;
var q = [];
q.push(root);
while (q.length > 0)
{
var count = q.length;
var product = 1;
while (count-- > 0)
{
var temp = q[0];
q.shift();
product = product * temp.data;
if (temp.left != null )
{
q.push(temp.left);
}
if (temp.right != null )
{
q.push(temp.right);
}
}
result = Math.max(product, result);
}
return result;
}
function newNode(data)
{
var node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
var 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);
document.write( "Maximum level product is " +
maxLevelProduct(root));
</script>
|
Output
Maximum level product is 160
Complexity Analysis:
- Time Complexity : O(n)
- Auxiliary Space : O(n)
Last Updated :
14 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...