Product of all nodes in a Binary Tree
Last Updated :
06 Sep, 2022
Given a Binary Tree. The task is to write a program to find the product of all of the nodes of the given binary tree.
In the above binary tree,
Product = 15*10*8*12*20*16*25 = 115200000
The idea is to recursively:
- Find the product of the left subtree.
- Find the product of the right subtree.
- Multiply the product of left and right subtrees with the current node’s data and return.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int key;
Node *left, *right;
};
Node* newNode( int key)
{
Node* node = new Node;
node->key = key;
node->left = node->right = NULL;
return (node);
}
int productBT(Node* root)
{
if (root == NULL)
return 1;
return (root->key * productBT(root->left) * productBT(root->right));
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->right = newNode(8);
int prod = productBT(root);
cout << "Product of all the nodes is: "
<< prod << endl;
return 0;
}
|
Java
import java.util.*;
class solution
{
static class Node {
int key;
Node left, right;
};
static Node newNode( int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null ;
return (node);
}
static int productBT(Node root)
{
if (root == null )
return 1 ;
return (root.key * productBT(root.left) * productBT(root.right));
}
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.left = newNode( 6 );
root.right.right = newNode( 7 );
root.right.left.right = newNode( 8 );
int prod = productBT(root);
System.out.println( "Product of all the nodes is: " +prod);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def productBT( root) :
if (root = = None ):
return 1
return (root.key * productBT(root.left) *
productBT(root.right))
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.left = newNode( 6 )
root.right.right = newNode( 7 )
root.right.left.right = newNode( 8 )
prod = productBT(root)
print ( "Product of all the nodes is:" , prod)
|
C#
using System;
class GFG
{
class Node
{
public int key;
public Node left, right;
};
static Node newNode( int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null ;
return (node);
}
static int productBT(Node root)
{
if (root == null )
return 1;
return (root.key * productBT(root.left) *
productBT(root.right));
}
public static void Main()
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
int prod = productBT(root);
Console.WriteLine( "Product of all " +
"the nodes is: " + prod);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .key = val;
this .left = null ;
this .right = null ;
}
}
function newNode(key)
{
var node = new Node();
node.key = key;
node.left = node.right = null ;
return (node);
}
function productBT(root)
{
if (root == null )
return 1;
return (root.key * productBT(root.left) * productBT(root.right));
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
var prod = productBT(root);
document.write( "Product of all the nodes is: " +prod);
</script>
|
Javascript
<script>
class Node {
constructor(val) {
this .key = val;
this .left = null ;
this .right = null ;
}
}
function newNode(key)
{
var node = new Node();
node.key = key;
node.left = node.right = null ;
return (node);
}
function productBT(root)
{
if (root == null )
return 1;
return (root.key * productBT(root.left) * productBT(root.right));
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
var prod = productBT(root);
document.write( "Product of all the nodes is: " +prod);
</script>
|
Output
Product of all the nodes is: 40320
Complexity Analysis
- Time complexity : O(n)
- As we are traversing the tree only once.
- Auxiliary Complexity: O(h)
- Here h is the height of the tree. The extra space is used in recursion call stack. In the worst case(when the tree is skewed) this can go upto O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...