Product of all leaf nodes of binary tree
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++
// CPP program to find product of // all leaf nodes of binary tree #include <bits/stdc++.h> using namespace std; // struct binary tree node struct Node { int data; Node *left, *right; }; // return new node Node* 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 nodes void 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 program int 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
// Java program to find product of // all leaf nodes of binary tree class GFG { // struct binary tree node static class Node { int data; Node left, right; }; // return new node static Node newNode( int data) { Node temp = new Node(); temp.data = data; temp.left = temp.right = null ; return temp; } // product static int prod = 1 ; // utility function which calculates // product of all leaf nodes static 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 program public 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 |
C#
// C# program to find product of // all leaf nodes of binary tree using System; class GFG { // struct binary tree node public class Node { public int data; public Node left, right; }; // return new node static Node newNode( int data) { Node temp = new Node(); temp.data = data; temp.left = temp.right = null ; return temp; } // product static int prod = 1; // utility function which calculates // product of all leaf nodes static 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 code public 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 |
1120
Time Complexity : O(n), where n is the total number of nodes in the tree.
Recommended Posts:
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Sum of all leaf nodes of binary tree
- Count Non-Leaf nodes in a Binary Tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Program to count leaf nodes in a binary tree
- Pairwise Swap leaf nodes in a binary tree
- Print all leaf nodes of a binary tree from right to left
- Print the nodes of binary tree as they become the leaf node
- Print leaf nodes in binary tree from left to right using one stack
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Iterative program to count leaf nodes in a Binary Tree
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Find height of a special binary tree whose leaf nodes are connected
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.