Find the parent node of maximum product Siblings in given Binary Tree
Given a binary tree, the task is to find the node whose children have maximum Sibling product in the given Binary Tree. If there are multiple such nodes, return the node which has the maximum value.
Examples:
Input: Tree:
4
/ \
5 2
/ \
3 1
/ \
6 12
Output: 3
Explanation: For the above tree, the maximum product for the siblings is formed for nodes 6 and 12 which are the children of node 3.
Input: Tree:
1
/ \
3 5
/ \ / \
6 9 4 8
Output: 3
Explanation: For the above tree, the maximum product for the siblings is formed for nodes 6 and 9 which are the children of node 3.
Approach using Level Order Traversal:
To solve this problem, level order traversal of the Binary Tree can be used to find the maximum sum of siblings. Follow the following steps:
- Start level order traversal of the tree from root of the tree.
- For each node, check if it has both the child.
- If yes, then find the node with maximum product of children and store this node value in a reference variable.
- Update the node value in reference variable if any node is found with greater product of children.
- If the current node don’t have both children, then skip that node
- Return the node value in reference variable, as it contains the node with maximum product of children, or the parent of maximum product siblings.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* getNode( int data)
{
Node* newNode
= (Node*) malloc ( sizeof (Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
int maxproduct(Node* root)
{
int mproduct = INT_MIN;
int ans = 0;
if (root == NULL
|| (root->left == NULL
&& root->right == NULL))
return 0;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* temp = q.front();
q.pop();
if (temp->right && temp->left) {
int curr_max
= temp->right->data
* temp->left->data;
if (mproduct < curr_max) {
mproduct = curr_max;
ans = temp->data;
}
else if (mproduct == curr_max) {
ans = max(ans, temp->data);
}
}
if (temp->right) {
q.push(temp->right);
}
if (temp->left) {
q.push(temp->left);
}
}
return ans;
}
int main()
{
Node* root = getNode(1);
root->left = getNode(3);
root->right = getNode(5);
root->left->left = getNode(6);
root->left->right = getNode(9);
root->right->left = getNode(4);
root->right->right = getNode(8);
cout << maxproduct(root) << endl;
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
class GFG {
static class Node {
int data;
Node left;
Node right;
public Node( int data) {
this .data = data;
this .left = null ;
this .right = null ;
}
};
public static Node getNode( int data) {
Node newNode = new Node(data);
newNode.data = data;
newNode.left = newNode.right = null ;
return newNode;
}
public static int maxproduct(Node root) {
int mproduct = Integer.MIN_VALUE;
int ans = 0 ;
if (root == null
|| (root.left == null
&& root.right == null ))
return 0 ;
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (!q.isEmpty()) {
Node temp = q.peek();
q.remove();
if (temp.right != null && temp.left != null ) {
int curr_max = temp.right.data
* temp.left.data;
if (mproduct < curr_max) {
mproduct = curr_max;
ans = temp.data;
} else if (mproduct == curr_max) {
ans = Math.max(ans, temp.data);
}
}
if (temp.right != null ) {
q.add(temp.right);
}
if (temp.left != null ) {
q.add(temp.left);
}
}
return ans;
}
public static void main(String args[]) {
Node root = getNode( 1 );
root.left = getNode( 3 );
root.right = getNode( 5 );
root.left.left = getNode( 6 );
root.left.right = getNode( 9 );
root.right.left = getNode( 4 );
root.right.right = getNode( 8 );
System.out.println(maxproduct(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def getNode(data):
newNode = Node(data)
return newNode
def maxproduct(root):
mproduct = 10 * * - 9
ans = 0 ;
if (root = = None or (root.left = = None and root.right = = None )):
return 0 ;
q = [];
q.append(root);
while ( len (q)):
temp = q[ 0 ];
q.pop( 0 );
if (temp.right and temp.left):
curr_max = temp.right.data * temp.left.data;
if (mproduct < curr_max):
mproduct = curr_max
ans = temp.data
elif (mproduct = = curr_max):
ans = max (ans, temp.data)
if (temp.right):
q.append(temp.right)
if (temp.left):
q.append(temp.left)
return ans
root = getNode( 1 );
root.left = getNode( 3 );
root.right = getNode( 5 );
root.left.left = getNode( 6 );
root.left.right = getNode( 9 );
root.right.left = getNode( 4 );
root.right.right = getNode( 8 );
print (maxproduct(root));
|
C#
using System;
using System.Collections.Generic;
public class GFG {
class Node {
public int data;
public Node left;
public Node right;
public Node( int data) {
this .data = data;
this .left = null ;
this .right = null ;
}
};
static Node getNode( int data) {
Node newNode = new Node(data);
newNode.data = data;
newNode.left = newNode.right = null ;
return newNode;
}
static int maxproduct(Node root) {
int mproduct = int .MinValue;
int ans = 0;
if (root == null
|| (root.left == null
&& root.right == null ))
return 0;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count!=0) {
Node temp = q.Peek();
q.Dequeue();
if (temp.right != null && temp.left != null ) {
int curr_max = temp.right.data
* temp.left.data;
if (mproduct < curr_max) {
mproduct = curr_max;
ans = temp.data;
} else if (mproduct == curr_max) {
ans = Math.Max(ans, temp.data);
}
}
if (temp.right != null ) {
q.Enqueue(temp.right);
}
if (temp.left != null ) {
q.Enqueue(temp.left);
}
}
return ans;
}
public static void Main(String []args) {
Node root = getNode(1);
root.left = getNode(3);
root.right = getNode(5);
root.left.left = getNode(6);
root.left.right = getNode(9);
root.right.left = getNode(4);
root.right.right = getNode(8);
Console.WriteLine(maxproduct(root));
}
}
|
Javascript
<script>
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
};
function getNode(data)
{
let newNode
= new Node(data);
return newNode;
}
function maxproduct(root) {
let mproduct = Number.MIN_VALUE;
let ans = 0;
if (root == null
|| (root.left == null
&& root.right == null ))
return 0;
let q = [];
q.push(root);
while (!q.length == 0) {
let temp = q[0];
q.shift();
if (temp.right && temp.left) {
let curr_max
= temp.right.data
* temp.left.data;
if (mproduct < curr_max) {
mproduct = curr_max;
ans = temp.data;
}
else if (mproduct == curr_max) {
ans = Math.max(ans, temp.data);
}
}
if (temp.right) {
q.push(temp.right);
}
if (temp.left) {
q.push(temp.left);
}
}
return ans;
}
let root = getNode(1);
root.left = getNode(3);
root.right = getNode(5);
root.left.left = getNode(6);
root.left.right = getNode(9);
root.right.left = getNode(4);
root.right.right = getNode(8);
document.write(maxproduct(root) + "<br>" );
</script>
|
Time Complexity: O(V) where V is the number of nodes in the tree.
Auxiliary Space: O(V).
- The idea is to traverse the tree in postorder fashion .
- Recursively , call the left subtree.
- Recursively , call the right subtree.
- Get , the product of left child and right child and update the max variable and max parent variable with parent root of left and right child.
- Here , max parent variable will be storing the parent node of the maximum product siblings.
Below is the Implementation of this Approach :-
C++
#include <climits>
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
Node( int data)
: data(data)
, left(nullptr)
, right(nullptr)
{
}
};
Node* getNode( int data)
{
Node* newNode = new Node(data);
newNode->data = data;
newNode->left = newNode->right = nullptr;
return newNode;
}
int parentNode = -1;
int maxProductVal = INT_MIN;
int helper(Node* root);
int calculateMaxProduct(Node* root)
{
helper(root);
return parentNode;
}
int helper(Node* root)
{
if (root == nullptr) {
return 0;
}
int left = helper(root->left);
int right = helper(root->right);
int currProduct = left * right;
if (maxProductVal < currProduct) {
maxProductVal = currProduct;
parentNode = root->data;
}
return root->data;
}
int main()
{
Node* root = getNode(1);
root->left = getNode(3);
root->right = getNode(5);
root->left->left = getNode(6);
root->left->right = getNode(9);
root->right->left = getNode(4);
root->right->right = getNode(8);
std::cout << calculateMaxProduct(root) << std::endl;
delete root->left->left;
delete root->left->right;
delete root->right->left;
delete root->right->right;
delete root->left;
delete root->right;
delete root;
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
class GFG {
static class Node {
int data;
Node left;
Node right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
};
public static Node getNode( int data)
{
Node newNode = new Node(data);
newNode.data = data;
newNode.left = newNode.right = null ;
return newNode;
}
static int parentNode = - 1 ;
static int maxProduct = Integer.MIN_VALUE;
public static int maxproduct(Node root)
{
helper(root);
return parentNode;
}
static int helper(Node root)
{
if (root == null ) {
return 0 ;
}
int left = helper(root.left);
int right = helper(root.right);
int currProduct = left * right;
if (maxProduct < currProduct) {
maxProduct = currProduct;
parentNode = root.data;
}
return root.data;
}
public static void main(String args[])
{
Node root = getNode( 1 );
root.left = getNode( 3 );
root.right = getNode( 5 );
root.left.left = getNode( 6 );
root.left.right = getNode( 9 );
root.right.left = getNode( 4 );
root.right.right = getNode( 8 );
System.out.println(maxproduct(root));
}
}
|
Python
import sys
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def get_node(data):
new_node = Node(data)
new_node.data = data
new_node.left = new_node.right = None
return new_node
parent_node = - 1
max_product_val = - sys.maxsize - 1
def helper(root):
global parent_node, max_product_val
if root is None :
return 0
left = helper(root.left)
right = helper(root.right)
curr_product = left * right
if max_product_val < curr_product:
max_product_val = curr_product
parent_node = root.data
return root.data
def calculate_max_product(root):
helper(root)
return parent_node
if __name__ = = "__main__" :
root = get_node( 1 )
root.left = get_node( 3 )
root.right = get_node( 5 )
root.left.left = get_node( 6 )
root.left.right = get_node( 9 )
root.right.left = get_node( 4 )
root.right.right = get_node( 8 )
print (calculate_max_product(root))
del root.left.left
del root.left.right
del root.right.left
del root.right.right
del root.left
del root.right
del root
|
C#
using System;
public class Node {
public int data;
public Node left;
public Node right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
public class BinaryTree {
private static int parentNode = -1;
private static int maxProductVal = int .MinValue;
private static int CalculateMaxProduct(Node root)
{
Helper(root);
return parentNode;
}
private static int Helper(Node root)
{
if (root == null ) {
return 0;
}
int left = Helper(root.left);
int right = Helper(root.right);
int currProduct = left * right;
if (maxProductVal < currProduct) {
maxProductVal = currProduct;
parentNode = root.data;
}
return root.data;
}
public static void Main()
{
Node root = new Node(1);
root.left = new Node(3);
root.right = new Node(5);
root.left.left = new Node(6);
root.left.right = new Node(9);
root.right.left = new Node(4);
root.right.right = new Node(8);
Console.WriteLine(CalculateMaxProduct(root));
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function getNode(data) {
const newNode = new Node(data);
newNode.data = data;
newNode.left = null ;
newNode.right = null ;
return newNode;
}
let parentNode = -1;
let maxProductVal = Number.MIN_SAFE_INTEGER;
function helper(root) {
if (root === null ) {
return 0;
}
const left = helper(root.left);
const right = helper(root.right);
const currProduct = left * right;
if (maxProductVal < currProduct) {
maxProductVal = currProduct;
parentNode = root.data;
}
return root.data;
}
function calculateMaxProduct(root) {
helper(root);
return parentNode;
}
const root = getNode(1);
root.left = getNode(3);
root.right = getNode(5);
root.left.left = getNode(6);
root.left.right = getNode(9);
root.right.left = getNode(4);
root.right.right = getNode(8);
console.log(calculateMaxProduct(root));
|
Time Complexity: O(N) , where N is the number of nodes in the tree
Auxiliary Space : O(N) , recursion stack space
Last Updated :
26 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...