Maximum product of any path in given Binary Tree
Last Updated :
04 Nov, 2022
Given a binary tree of N nodes, the task is to find the maximum product of the elements of any path in the binary tree.
Note: A path starts from the root and ends at any leaf in the tree.
Examples:
Input:
4
/ \
2 8
/ \ / \
2 1 3 4
Output: 128
Explanation: Path in the given tree goes like {4, 8, 4} which gives the max score of 4 x 8 x 4 = 128.
Input:
10
/ \
7 5
\
1
Output: 70
Explanation: The path {10, 7} gives a score of 70 which is the maximum possible.
Approach: The idea to solve the problem is by using DFS traversal of a tree using recursion.
For every node recursively find the maximum product of left subtree and right subtree of that node and return the product of that value with the node’s data.
Follow the steps mentioned below to solve the problem
- As the base Condition. If the root is NULL, simply return 1.
- Call the recursive function for the left and right subtrees to get the maximum product from both the subtrees.
- Return the value of the current node multiplied by the maximum product out of the left and right subtree as the answer of the current recursion.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node( int val) { this ->data = val; }
};
long long findMaxScore(Node* root)
{
if (root == 0)
return 1;
return root->data
* max(findMaxScore(root->left),
findMaxScore(root->right));
}
int main()
{
Node* root = new Node(4);
root->left = new Node(2);
root->right = new Node(8);
root->left->left = new Node(3);
root->left->right = new Node(1);
root->right->left = new Node(3);
root->right->right = new Node(4);
cout << findMaxScore(root) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static class Node {
int data;
Node left;
Node right;
Node( int val) { this .data = val; }
}
public static long findMaxScore(Node root)
{
if (root == null )
return 1 ;
return root.data
* Math.max(findMaxScore(root.left),
findMaxScore(root.right));
}
public static void main(String[] args)
{
Node root = new Node( 4 );
root.left = new Node( 2 );
root.right = new Node( 8 );
root.left.left = new Node( 3 );
root.left.right = new Node( 1 );
root.right.left = new Node( 3 );
root.right.right = new Node( 4 );
System.out.println(findMaxScore(root));
}
}
|
Python3
class Node:
def __init__( self ,d):
self .data = d
self .left = None
self .right = None
def findMaxScore(root):
if (root = = None ):
return 1
return root.data * max (findMaxScore(root.left),findMaxScore(root.right))
root = Node( 4 )
root.left = Node( 2 )
root.right = Node( 8 )
root.left.left = Node( 3 )
root.left.right = Node( 1 )
root.right.left = Node( 3 )
root.right.right = Node( 4 )
print (findMaxScore(root))
|
C#
using System;
class GFG
{
public class Node {
public int data;
public Node left;
public Node right;
public Node( int val) { this .data = val; }
}
public static long findMaxScore(Node root)
{
if (root == null )
return 1;
return root.data
* Math.Max(findMaxScore(root.left),
findMaxScore(root.right));
}
public static void Main()
{
Node root = new Node(4);
root.left = new Node(2);
root.right = new Node(8);
root.left.left = new Node(3);
root.left.right = new Node(1);
root.right.left = new Node(3);
root.right.right = new Node(4);
Console.WriteLine(findMaxScore(root));
}
}
|
Javascript
<script>
class Node
{
constructor(d)
{
this .data = d;
this .left = null ;
this .right = null ;
}
};
function findMaxScore(root)
{
if (root == null )
return 1;
return root.data
* Math.max(findMaxScore(root.left),
findMaxScore(root.right));
}
let root = new Node(4);
root.left = new Node(2);
root.right = new Node(8);
root.left.left = new Node(3);
root.left.right = new Node(1);
root.right.left = new Node(3);
root.right.right = new Node(4);
document.write(findMaxScore(root) + '<br>' );
</script>
|
Time Complexity: O(N).
Auxiliary Space: O( Height of the tree)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...