# Find product of sums of data of leaves at same levels | Set 2

Given a Binary Tree, return following value for it.

- For every level, compute sum of all leaves if there are leaves at this level. Otherwise ignore it.
- Return multiplication of all sums.

**Examples**:

Input: Root of below tree 2 / \ 7 5 \ 9Output: 63 First levels doesn't have leaves. Second level has one leaf 7 and third level also has one leaf 9. Therefore result is 7*9 = 63Input: Root of below tree 2 / \ 7 5 / \ \ 8 6 9 / \ / \ 1 11 4 10Output: 208 First two levels don't have leaves. Third level has single leaf 8. Last level has four leaves 1, 11, 4 and 10. Therefore result is 8 * (1 + 11 + 4 + 10)

In the previous article we have seen a Queue based solution using level order traversal.

Here, we are simply doing preorder traversal of the binary tree, and we have used unordered_map in C++ STL to store sum of leaf nodes at same level. Then in a single traversal of the map, we’ve calculated the final product of level sums.

Below is the implementation of above approach:

`// C++ program to find product of sums ` `// of data of leaves at same levels ` `// using map in STL ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Binary Tree Node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `Node* left; ` ` ` `Node* right; ` `}; ` ` ` `// Utitlity funtion to create ` `// a new Tree node ` `Node* newNode(` `int` `data) ` `{ ` ` ` `Node* temp = ` `new` `Node; ` ` ` `temp->data = data; ` ` ` `temp->left = temp->right = NULL; ` ` ` `return` `temp; ` `} ` ` ` `// Helper function to calculate sum of ` `// leaf nodes at same level and store in ` `// an unordered_map ` `void` `productOfLevelSumUtil(Node* root, ` ` ` `unordered_map<` `int` `, ` `int` `>& level_sum, ` ` ` `int` `level) ` `{ ` ` ` ` ` `if` `(!root) ` ` ` `return` `; ` ` ` ` ` `// Check if current node is a leaf node ` ` ` `// If yes add it to sum of current level ` ` ` `if` `(!root->left && !root->right) ` ` ` `level_sum[level] += root->data; ` ` ` ` ` `// Traverse left subtree ` ` ` `productOfLevelSumUtil(root->left, level_sum, ` ` ` `level + 1); ` ` ` ` ` `// Traverse right subtree ` ` ` `productOfLevelSumUtil(root->right, level_sum, ` ` ` `level + 1); ` `} ` ` ` `// Function to calcuate product of level sums ` `int` `productOfLevelSum(Node* root) ` `{ ` ` ` `// Create a map to store sum of leaf ` ` ` `// nodes at same levels. ` ` ` `unordered_map<` `int` `, ` `int` `> level_sum; ` ` ` ` ` `// Call the helper function to ` ` ` `// calculate level sums of leaf nodes ` ` ` `productOfLevelSumUtil(root, level_sum, 0); ` ` ` ` ` `// variable to store final product ` ` ` `int` `prod = 1; ` ` ` ` ` `// Traverse the map to calculate product ` ` ` `// of level sums ` ` ` `for` `(` `auto` `it = level_sum.begin(); ` ` ` `it != level_sum.end(); it++) ` ` ` `prod *= it->second; ` ` ` ` ` `// Return the result ` ` ` `return` `prod; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Creating Binary Tree ` ` ` `Node* root = newNode(2); ` ` ` `root->left = newNode(7); ` ` ` `root->right = newNode(5); ` ` ` `root->left->right = newNode(6); ` ` ` `root->left->left = newNode(8); ` ` ` `root->left->right->left = newNode(1); ` ` ` `root->left->right->right = newNode(11); ` ` ` `root->right->right = newNode(9); ` ` ` `root->right->right->left = newNode(4); ` ` ` `root->right->right->right = newNode(10); ` ` ` ` ` `cout << ` `"Final product is = "` ` ` `<< productOfLevelSum(root) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Final product is = 208

**Time Complexity**: O(N)

**Auxiliary Space**: O(N)

Where N is the number of nodes in the Binary Tree.

## Recommended Posts:

- Find multiplication of sums of data of leaves at same levels
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Find sum of all right leaves in a given Binary Tree
- Find first non matching leaves in two binary trees
- Find sum of all left leaves in a given Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Find pairs in array whose sums already exist in array
- Find the Number of Maximum Product Quadruples
- Find pair with greatest product in array
- Find maximum level product in Binary Tree
- Check if all leaves are at same level
- Minimum sum path between two leaves of a binary tree
- Height of binary tree considering even level leaves only
- Print all nodes in a binary tree having K leaves
- Count of nodes which are at a distance X from root and leaves

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.