Given a Binary Tree consisting of N nodes, the task is to replace each node of the tree with the product of all the remaining nodes.
Examples:
Input:
1
/ \
2 3
/ \
4 5
Output:
120
/ \
60 40
/ \
30 24
Input:
2
/ \
2 3
Output:
6
/ \
6 4
Approach: The given problem can be solved by first calculating the product of all the nodes in the given Tree and then perform any tree traversal on the given tree and update each node by the value (P/(root->values). Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node *left, *right;
Node( int data)
{
this ->data = data;
left = NULL;
right = NULL;
}
};
Node* newNode( int value)
{
Node* temp = new Node(value);
return (temp);
}
int findProduct(Node* root)
{
if (root == NULL)
return 1;
return (root->data
* findProduct(root->left)
* findProduct(root->right));
}
void display(Node* root)
{
if (root == NULL)
return ;
display(root->left);
cout << root->data << " " ;
display(root->right);
}
void convertTree( int product,
Node* root)
{
if (root == NULL)
return ;
root->data = product / (root->data);
convertTree(product, root->left);
convertTree(product, root->right);
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->right->left = newNode(4);
root->right->right = newNode(5);
cout << "Inorder Traversal of "
<< "given Tree:\n" ;
display(root);
int product = findProduct(root);
cout << "\nInorder Traversal of "
<< "given Tree:\n" ;
convertTree(product, root);
display(root);
return 0;
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self .left = None
self .right = None
def findProduct(root):
if (root = = None ):
return 1
return (root.data * findProduct(root.left) *
findProduct(root.right))
def display(root):
if (root = = None ):
return
display(root.left)
print (root.data, end = " " )
display(root.right)
def convertTree(product, root):
if (root = = None ):
return
root.data = product / / (root.data)
convertTree(product, root.left)
convertTree(product, root.right)
if __name__ = = '__main__' :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.right.left = Node( 4 )
root.right.right = Node( 5 )
print ( "Inorder Traversal of given Tree: " )
display(root)
product = findProduct(root)
print ( "\nInorder Traversal of given Tree:" )
convertTree(product, root)
display(root)
|
Java
public class GFG {
static class Node {
public int data;
public Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.data = key;
temp.left = temp.right = null ;
return temp;
}
static int findProduct(Node root)
{
if (root == null )
return 1 ;
return (root.data * findProduct(root.left)
* findProduct(root.right));
}
static void display(Node root)
{
if (root == null )
return ;
display(root.left);
System.out.print(root.data + " " );
display(root.right);
}
static void convertTree( int product, Node root)
{
if (root == null )
return ;
root.data = product / (root.data);
convertTree(product, root.left);
convertTree(product, root.right);
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.right.left = newNode( 4 );
root.right.right = newNode( 5 );
System.out.println( "Inorder Traversal of "
+ "given Tree:" );
display(root);
int product = findProduct(root);
System.out.println( "\nInorder Traversal of "
+ "given Tree:" );
convertTree(product, root);
display(root);
}
}
|
C#
using System;
public class GFG {
class Node {
public int data;
public Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.data = key;
temp.left = temp.right = null ;
return temp;
}
static int findProduct(Node root)
{
if (root == null )
return 1;
return (root.data * findProduct(root.left)
* findProduct(root.right));
}
static void display(Node root)
{
if (root == null )
return ;
display(root.left);
Console.Write(root.data + " " );
display(root.right);
}
static void convertTree( int product, Node root)
{
if (root == null )
return ;
root.data = product / (root.data);
convertTree(product, root.left);
convertTree(product, root.right);
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.right.left = newNode(4);
root.right.right = newNode(5);
Console.WriteLine( "Inorder Traversal of "
+ "given Tree:" );
display(root);
int product = findProduct(root);
Console.WriteLine( "\nInorder Traversal of "
+ "given Tree:" );
convertTree(product, root);
display(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function newNode(key) {
var temp = new Node();
temp.data = key;
temp.left = temp.right = null ;
return temp;
}
function findProduct(root) {
if (root == null ) return 1;
return root.data * findProduct(root.left) * findProduct(root.right);
}
function display(root) {
if (root == null ) return ;
display(root.left);
document.write(root.data + " " );
display(root.right);
}
function convertTree(product, root) {
if (root == null ) return ;
root.data = parseInt(product / root.data);
convertTree(product, root.left);
convertTree(product, root.right);
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.right.left = newNode(4);
root.right.right = newNode(5);
document.write( "Inorder Traversal of " + "given Tree: <br>" );
display(root);
var product = findProduct(root);
document.write( "<br>Inorder Traversal of " + "given Tree:<br>" );
convertTree(product, root);
display(root);
</script>
|
Output:
Inorder Traversal of given Tree:
2 1 4 3 5
Inorder Traversal of given Tree:
60 120 30 40 24
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Jun, 2021
Like Article
Save Article