Min-Max Product Tree of a given Binary Tree

Given a Binary Tree, the task is to convert the given Binary tree into Min-Max Product Tree and print the level-order sequence of the modified tree.
Min-Max Product Tree: A Min-Max Product Tree contains the product of the minimum and maximum values of its left and right subtrees at each node.
Note: For any node having only a single child, the value of that child node will be considered as both the minimum and the maximum.

Examples:

Input:  
                 1
                /  \ 
              12    11
             /     /   \ 
            3     4     13 
                   \    / 
                   15  5  
Output: 
45 9 60 3 225 25 15 5
Explanation:  
Min-Max Product Tree:
                 45 (3 * 15)
                /  \ 
       ( 3 * 3)9    60 (4 * 15)
             /     /   \ 
            3     225   25 (5* 5)
                   \    / 
                   15  5

Input:
                  5
                /  \ 
              21     77 
             /  \      \
            61   16     16 
                  \    / 
                   10  3    
                   /
                  23 
Output:
231 610 48 61 230 9 529 3 23

Approach:
The idea is to use the post order traversal to traverse the left and right subtree recursively and extract the minimum and maximum. Store the product of minimum and maximum for every node in the Min-Max Product tree. Once, computed, compare the current node value with the current minimum and maximum and modify accordingly. Return the new minimum and maximum for the node at a higher level. Repeat this process for all nodes and finally, print the level order traversal of the Min-Max Product tree.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// A Tree node
struct Node {
    int data;
    struct Node *left, *right;
};
  
// Utility function to create
// a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->left = temp->right = NULL;
    return (temp);
}
  
// A minMax Structre for storing
// minimum and maximum value
struct minMax {
    int min;
    int max;
};
  
// Function to return min value
int min(minMax* a, minMax* b)
{
    if (a != NULL
        && b != NULL) {
  
        return a->min < b->min
                   ? a->min
                   : b->min;
    }
  
    return a == NULL
               ? b->min
               : a->min;
}
  
// Function to return max value
int max(minMax* a, minMax* b)
{
    if (a != NULL
        && b != NULL) {
  
        return a->max > b->max
                   ? a->max
                   : b->max;
    }
  
    return a == NULL
               ? b->max
               : a->max;
}
  
// Function to return min value
int min(int x, int y)
{
    return x < y ? x : y;
}
  
// Function to return max value
int max(int x, int y)
{
    return x > y ? x : y;
}
  
// Utility function to create
// min-max product tree
minMax* minMaxTreeUtil(
    Node* root)
{
  
    // Base condition
    if (root == NULL) {
        return NULL;
    }
  
    minMax* var = new minMax;
  
    // Condition to check if
    // current node is leaf node
    if (root->left == NULL
        && root->right == NULL) {
  
        var->min = root->data;
        var->max = root->data;
        return var;
    }
  
    // Left recursive call
    minMax* left
        = minMaxTreeUtil(root->left);
  
    // Right recursive call
    minMax* right
        = minMaxTreeUtil(root->right);
  
    // Store Min
    var->min = min(left, right);
  
    // Store Max
    var->max = max(left, right);
  
    // Store current node data
    int currData = root->data;
  
    // Assign product of minimum
    // and maximum value
    root->data = var->min * var->max;
  
    // Again store min by considering
    // current node value
    var->min = min(var->min, currData);
  
    // Again store max by considering
    // current node value
    var->max = max(var->max, currData);
    return var;
}
void print(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
  
    // Create an empty queue for
    // level order tarversal
    queue<Node*> q;
  
    // Enqueue Root and initialize
    // height
    q.push(root);
  
    while (q.empty() == false) {
  
        // nodeCount (queue size)
        // indicates number
        // of nodes at current level.
        int nodeCount = q.size();
  
        // Dequeue all nodes of current
        // level and Enqueue all nodes
        // of next level
        while (nodeCount > 0) {
  
            Node* node = q.front();
            cout << node->data << " ";
            q.pop();
            if (node->left != NULL)
                q.push(node->left);
            if (node->right != NULL)
                q.push(node->right);
            nodeCount--;
        }
    }
}
  
void minMaxProductTree(Node* root)
{
    // Utility Function call
    minMaxTreeUtil(root);
  
    // Print tree
    print(root);
}
  
// Driver Code
int main()
{
  
    /*     10 
            / \ 
        48     3 
                / \ 
            11     37 
            / \ / \ 
            7 29 42 19 
                    
                    
    */
  
    // Create Binary Tree as shown
    Node* root = newNode(10);
    root->left = newNode(48);
    root->right = newNode(3);
  
    root->right->left = newNode(11);
    root->right->right = newNode(37);
    root->right->left->left = newNode(7);
    root->right->left->right = newNode(29);
    root->right->right->left = newNode(42);
    root->right->right->right = newNode(19);
    root->right->right->right->left = newNode(7);
  
    // Create Min Max Product Tree
    minMaxProductTree(root);
  
    return 0;
}

chevron_right


Output:

144 48 294 203 294 7 29 42 49 7


Time Complexity: O(N), where N denotes the number of nodes in the binary tree
Auxillary Space: O(1)

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

Check out this Author's contributed articles.

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.