Skip to content
Related Articles

Related Articles

Find product of sums of data of leaves at same levels | Set 2
  • Last Updated : 22 Nov, 2019

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
             \
              9
Output: 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 = 63

Input: Root of below tree
         2
       /   \
     7      5
    / \      \
   8   6      9
      / \    /  \
     1  11  4    10 

Output: 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 function 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 calculate 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;
}
Output:
Final product is = 208

Time Complexity: O(N)
Auxiliary Space: O(N)
Where N is the number of nodes in the Binary Tree.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :