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*20*812*16*25 = 974400000
The idea is to recursively:
- Find product of left subtree.
- Find product of right subtree.
- Mutiply the product of left and right subtrees with current node’s data and return.
Below is the implementation of the above approach:
C++
// Program to print product of all // the nodes of a binary tree #include <iostream> using namespace std; // Binary Tree Node struct Node { int key; Node *left, *right; }; /* utility that allocates a new Node with the given key */Node* newNode(int key) { Node* node = new Node; node->key = key; node->left = node->right = NULL; return (node); } // Function to find product of // all the nodes int productBT(Node* root) { if (root == NULL) return 1; return (root->key * productBT(root->left) * productBT(root->right)); } // Driver Code int main() { // Binary Tree is: // 1 // / \ // 2 3 // / \ / \ // 4 5 6 7 // \ // 8 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; } |
chevron_right
filter_none
Java
// Java Program to print product of all // the nodes of a binary tree import java.util.*; class solution { // Binary Tree Node static class Node { int key; Node left, right; }; /* utility that allocates a new Node with the given key */static Node newNode(int key) { Node node = new Node(); node.key = key; node.left = node.right = null; return (node); } // Function to find product of // all the nodes static int productBT(Node root) { if (root == null) return 1; return (root.key * productBT(root.left) * productBT(root.right)); } // Driver Code public static void main(String args[]) { // Binary Tree is: // 1 // / \ // 2 3 // / \ / \ // 4 5 6 7 // \ // 8 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); } } //contributed by Arnab Kundu |
chevron_right
filter_none
Time complexity : O(n)
Recommended Posts:
- Product of all leaf nodes of binary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Product of nodes at k-th level in a tree represented as string
- Sum of all nodes in a binary tree
- XOR of path between any two nodes in a Binary Tree
- Sink Odd nodes in Binary Tree
- Sum of all leaf nodes of binary tree
- Find maximum among all right nodes in Binary Tree
- Find sum of all nodes of the given perfect binary tree
- Check if two nodes are cousins in a Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Given a binary tree, how do you remove all the half nodes?
- Print nodes in top view of Binary Tree | Set 2
- Print all odd nodes of Binary Search Tree
- Print all nodes between two given levels in Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



