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.
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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code