Product of all leaf nodes of binary tree
Last Updated :
06 Sep, 2022
Given a binary tree, find the product of all the leaf nodes.
Examples:
Input :
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Output :
product = 4 * 5 * 8 * 7 = 1120
The idea is to traverse the tree in any fashion and check if the node is the leaf node or not. If the node is the leaf node, multiply node data to a variable prod used to store the products of leaf nodes.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void leafProduct(Node* root, int & prod)
{
if (!root)
return ;
if (!root->left && !root->right)
prod *= root->data;
leafProduct(root->left, prod);
leafProduct(root->right, prod);
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right = newNode(3);
root->right->right = newNode(7);
root->right->left = newNode(6);
root->right->left->right = newNode(8);
int prod = 1;
leafProduct(root, prod);
cout << prod << endl;
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static int prod = 1 ;
static void leafProduct(Node root )
{
if (root == null )
return ;
if (root.left == null && root.right == null )
prod *= root.data;
leafProduct(root.left);
leafProduct(root.right);
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right = newNode( 3 );
root.right.right = newNode( 7 );
root.right.left = newNode( 6 );
root.right.left.right = newNode( 8 );
prod = 1 ;
leafProduct(root);
System.out.println(prod );
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode( data) :
temp = Node( 0 )
temp.data = data
temp.left = temp.right = None
return temp
prod = 1
def leafProduct( root ) :
global prod
if (root = = None ) :
return
if (root.left = = None and root.right = = None ):
prod * = root.data
leafProduct(root.left)
leafProduct(root.right)
root = newNode( 1 )
root.left = newNode( 2 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right = newNode( 3 )
root.right.right = newNode( 7 )
root.right.left = newNode( 6 )
root.right.left.right = newNode( 8 )
prod = 1
leafProduct(root)
print (prod )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static int prod = 1;
static void leafProduct(Node root )
{
if (root == null )
return ;
if (root.left == null && root.right == null )
prod *= root.data;
leafProduct(root.left);
leafProduct(root.right);
}
public static void Main(String []args)
{
Node root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right = newNode(3);
root.right.right = newNode(7);
root.right.left = newNode(6);
root.right.left.right = newNode(8);
prod = 1;
leafProduct(root);
Console.WriteLine(prod );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function newNode(data) {
var temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
var prod = 1;
function leafProduct(root) {
if (root == null )
return ;
if (root.left == null && root.right == null )
prod *= root.data;
leafProduct(root.left);
leafProduct(root.right);
}
var root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right = newNode(3);
root.right.right = newNode(7);
root.right.left = newNode(6);
root.right.left.right = newNode(8);
prod = 1;
leafProduct(root);
document.write(prod);
</script>
|
Time Complexity: O(n), where n is the total number of nodes in the tree.
Share your thoughts in the comments
Please Login to comment...