Given a Binary Tree having positive and negative nodes, the task is to find maximum product level in it.
Input : 4 / \ 2 -5 / \ /\ -1 3 -2 6 Output: 36 Explanation : Product of all nodes of 0'th level is 4 Product of all nodes of 1'th level is -10 Product of all nodes of 0'th level is 36 Hence maximum product is 6 Input : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 Output : 160 Explanation : Product of all nodes of 0'th level is 1 Product of all nodes of 1'th level is 6 Product of all nodes of 0'th level is 160 Product of all nodes of 0'th level is 42 Hence maximum product is 160
Prerequisites: Maximum Width of a Binary Tree
Approach : The idea is to do level order traversal of tree. While doing traversal, process nodes of different level separately. For every level being processed, compute product of nodes in the level and keep track of maximum product.
Maximum level product is 160
Time Complexity : O(n)
Auxiliary Space : O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Convert a Binary Tree into its Mirror Tree
- Balanced expressions such that given positions have opening brackets | Set 2
- Python | Convert a nested list into a flat list
- Sum of elements of all partitions of number such that no element is less than K
- Bank Of America (BA Continuum India Pvt. Ltd.) Campus Recruitment
- Tail recursion to calculate sum of array elements.
- Water Jug Problem using Memoization
- Modular exponentiation (Recursive)
- Reduce a number to 1 by performing given operations
- Java 8 | Arrays parallelSort() method with Examples