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.
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
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:
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.
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Product of all nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Find maximum level product in Binary Tree
- Sum and Product of maximum and minimum element in Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Minimum swap required to convert binary tree to binary search tree
- Sum and Product of minimum and maximum element of Binary Search Tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a given Binary tree to a tree that holds Logical OR property
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.