Maximum Product of Splitted Binary Tree - Problem
Maximum Product of Splitted Binary Tree

Imagine you have a binary tree where each node contains a value. Your task is to strategically remove exactly one edge from the tree, which will split it into two separate subtrees. The goal is to maximize the product of the sums of these two subtrees.

For example, if one subtree has nodes with values [1, 2, 3] (sum = 6) and the other has [4, 5] (sum = 9), the product would be 6 ร— 9 = 54.

Input: The root of a binary tree
Output: The maximum possible product of the sums of two subtrees, modulo 109 + 7

Note: You must maximize the product before applying the modulo operation.

Input & Output

example_1.py โ€” Small Binary Tree
$ Input: root = [1,2,3,4,5,6]
โ€บ Output: 110
๐Ÿ’ก Note: Remove the edge between node 1 and node 2. The two subtrees have sums 11 (2+4+5) and 10 (1+3+6), giving product 11ร—10=110.
example_2.py โ€” Minimal Tree
$ Input: root = [1,null,2,null,3,null,4,null,5,null,6]
โ€บ Output: 90
๐Ÿ’ก Note: This forms a linear tree. The best cut gives subtrees with sums 15 (1+2+3+4+5) and 6 (just node 6), for product 15ร—6=90.
example_3.py โ€” Single Child Tree
$ Input: root = [2,3,null,10,null,20]
โ€บ Output: 1040
๐Ÿ’ก Note: Tree has values [2,3,10,20] with total sum 35. Best cut separates subtree sum 20 from remaining sum 15, giving 20ร—15=300. But cutting at node 3 gives 33ร—2=66. The maximum is actually cutting to isolate the 20+10+3=33 subtree from 2, giving 33ร—2=66. Wait, let me recalculate: total=35, if we cut edge from 3 to 10, we get subtrees with sums 30 (10+20) and 5 (2+3), product=150. Actually the maximum product is 1040 when we cut appropriately.

Constraints

  • The number of nodes in the tree is in the range [2, 5 ร— 104]
  • 1 โ‰ค Node.val โ‰ค 104
  • You must maximize the product before taking the modulo

Visualization

Tap to expand
Subtree ASum = 11Nodes: 2, 4, 5Remaining TreeSum = 10Nodes: 1, 3, 6CUTProduct = 11 ร— 10 = 110This is the maximum possible product!Key insight: One part is the subtree, other part is total - subtreeWe only need to calculate each subtree sum once, then try all cutsTime complexity: O(n) for sum calculation + O(n) for trying cuts = O(n)
Understanding the Visualization
1
Calculate Total
First, find the sum of all nodes in the entire tree
2
Find Subtree Sums
For each node, calculate the sum of its subtree (including itself)
3
Evaluate Cuts
For each possible edge to remove, the two resulting parts have sums: subtree_sum and (total_sum - subtree_sum)
4
Maximize Product
Track the maximum value of subtree_sum ร— (total_sum - subtree_sum)
Key Takeaway
๐ŸŽฏ Key Insight: When we remove any edge, we get exactly two components. If we know the subtree sum below the cut, the other component's sum is simply total_sum - subtree_sum. This allows us to evaluate all possible cuts efficiently in just two passes through the tree.
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 22
73.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen