Product of all leaf nodes of binary tree - GeeksforGeeks

27 May, 2021
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.Following is the implementation of above approach.  
C++
Java
Python
C#
Javascript
// CPP program to find product of// all leaf nodes of binary tree#include <bits/stdc++.h>using namespace std; // struct binary tree nodestruct Node {    int data;    Node *left, *right;}; // return new nodeNode* newNode(int data){    Node* temp = new Node();    temp->data = data;    temp->left = temp->right = NULL;    return temp;} // utility function which calculates// product of all leaf nodesvoid leafProduct(Node* root, int& prod){    if (!root)        return;     // product root data to prod if    // root is a leaf node    if (!root->left && !root->right)        prod *= root->data;     // propagate recursively in left    // and right subtree    leafProduct(root->left, prod);    leafProduct(root->right, prod);} // Driver programint main(){     // construct binary tree    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);     // variable to store product of leaf nodes    int prod = 1;    leafProduct(root, prod);    cout << prod << endl;    return 0;}
// Java program to find product of// all leaf nodes of binary treeclass GFG{     // struct binary tree nodestatic class Node{    int data;    Node left, right;}; // return new nodestatic Node newNode(int data){    Node temp = new Node();    temp.data = data;    temp.left = temp.right = null;    return temp;} // productstatic int prod = 1; // utility function which calculates// product of all leaf nodesstatic void leafProduct(Node root ){    if (root == null)        return;     // product root data to prod if    // root is a leaf node    if (root.left == null && root.right == null)        prod *= root.data;     // propagate recursively in left    // and right subtree    leafProduct(root.left);    leafProduct(root.right);} // Driver programpublic static void main(String args[]){     // construct binary tree    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);     // variable to store product of leaf nodes    prod = 1;    leafProduct(root);    System.out.println(prod );}} // This code is contributed by Arnab Kundu
# Python program to find product of# all leaf nodes of binary tree # Node classclass Node:         # Function to initialise the node object    def __init__(self, data):        self.data = data # Assign data        self.next =None # return new nodedef newNode( data) :     temp = Node(0)    temp.data = data    temp.left = temp.right = None    return temp # productprod = 1 # utility function which calculates# product of all leaf nodesdef leafProduct( root ) :     global prod    if (root == None) :        return     # product root data to prod if    # root is a leaf node    if (root.left == None and root.right == None):        prod *= root.data     # propagate recursively in left    # and right subtree    leafProduct(root.left)    leafProduct(root.right) # Driver program # construct binary treeroot = 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) # variable to store product of leaf nodesprod = 1leafProduct(root)print(prod ) # This code is contributed by Arnab Kundu
// C# program to find product of// all leaf nodes of binary treeusing System; class GFG{     // struct binary tree nodepublic class Node{    public int data;    public Node left, right;}; // return new nodestatic Node newNode(int data){    Node temp = new Node();    temp.data = data;    temp.left = temp.right = null;    return temp;} // productstatic int prod = 1; // utility function which calculates// product of all leaf nodesstatic void leafProduct(Node root ){    if (root == null)        return;     // product root data to prod if    // root is a leaf node    if (root.left == null && root.right == null)        prod *= root.data;     // propagate recursively in left    // and right subtree    leafProduct(root.left);    leafProduct(root.right);} // Driver codepublic static void Main(String []args){     // construct binary tree    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);     // variable to store product of leaf nodes    prod = 1;    leafProduct(root);    Console.WriteLine(prod );}} // This code has been contributed by 29AjayKumar
<script>// javascript program to find product of// all leaf nodes of binary tree// struct binary tree nodeclass Node {    constructor(val) {        this.data = val;        this.left = null;        this.right = null;    }}     // return new node    function newNode(data) {var temp = new Node();        temp.data = data;        temp.left = temp.right = null;        return temp;    }     // product    var prod = 1;     // utility function which calculates    // product of all leaf nodes    function leafProduct(root) {        if (root == null)            return;         // product root data to prod if        // root is a leaf node        if (root.left == null && root.right == null)            prod *= root.data;         // propagate recursively in left        // and right subtree        leafProduct(root.left);        leafProduct(root.right);    }     // Driver program              // construct binary treevar 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);         // variable to store product of leaf nodes        prod = 1;        leafProduct(root);        document.write(prod); // This code contributed by gauravrajput1</script>
1120
 
Time Complexity : O(n), where n is the total number of nodes in the tree. 
andrew1234
29AjayKumar
Akanksha_Rai
GauravRajput1
Technical Scripter 2018
tree-traversal
Technical Scripter
Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
AVL Tree | Set 1 (Insertion)
Level Order Binary Tree Traversal
Binary Tree | Set 3 (Types of Binary Tree)
Inorder Tree Traversal without Recursion
Write a Program to Find the Maximum Depth or Height of a Tree
Binary Tree | Set 2 (Properties)
Decision Tree
A program to check if a binary tree is BST or not