Maximum Product of Splitted Binary Tree (LeetCode 1339): Solution and Explanation
Remove one edge so the two resulting subtree sums have the largest product. Every cut splits off one node's subtree, so one DFS that records subtree sums turns the problem into a scan over s * (T - s).
Intuition
Removing the edge between a node and its parent detaches exactly that node's subtree. The other piece is everything else. So each possible cut corresponds to one non-root node.
If the detached subtree sums to s and the whole tree sums to T, the remaining piece sums to T - s and the product is s * (T - s). That means the only thing you need per node is its subtree sum.
A post-order DFS gives every subtree sum in one pass: a node's sum is its value plus its children's sums. The root's sum is T, and a second scan over the stored sums finds the best product. The product reaches about 6 * 10^16, so it needs 64-bit integers, and the modulo is applied only to the final maximum.
Approach: Subtree sums plus a scan
- Run a post-order DFS. For each node compute
s = val + sum(left) + sum(right)and appendsto a list. - The DFS result for the root is the total
T. - For every stored
s, computes * (T - s)with 64-bit arithmetic and keep the largest. - Return the largest product modulo
10^9 + 7.
Step-by-step example
root = [1,2,3,4,5,6]
- Post-order sums: leaf 4 gives 4, leaf 5 gives 5, node 2 gives 2 + 4 + 5 = 11, leaf 6 gives 6, node 3 gives 3 + 6 + 0 = 9, root gives 1 + 11 + 9 = 21.
- T = 21. Try each stored sum: 4 * 17 = 68, 5 * 16 = 80, 11 * 10 = 110, 6 * 15 = 90, 9 * 12 = 108.
- The root's own sum 21 gives 21 * 0 = 0, which never wins.
- The best product is 110, from cutting the edge above node 2. It is below 10^9 + 7, so the answer is 110.
Code: Python, Java, C++
1class Solution:
2 def maxProduct(self, root: Optional[TreeNode]) -> int:
3 sums = []
4
5 def subtree(node):
6 if not node:
7 return 0
8 s = node.val + subtree(node.left) + subtree(node.right)
9 sums.append(s)
10 return s
11
12 total = subtree(root)
13 best = 0
14 for s in sums:
15 best = max(best, s * (total - s))
16 return best % (10**9 + 7)1class Solution {
2 private List<Long> sums;
3
4 public int maxProduct(TreeNode root) {
5 sums = new ArrayList<>();
6 long total = subtree(root);
7 long best = 0;
8 for (long s : sums) {
9 best = Math.max(best, s * (total - s));
10 }
11 return (int) (best % 1_000_000_007L);
12 }
13
14 private long subtree(TreeNode node) {
15 if (node == null) return 0;
16 long s = node.val + subtree(node.left) + subtree(node.right);
17 sums.add(s);
18 return s;
19 }
20}1class Solution {
2 vector<long long> sums;
3
4 long long subtree(TreeNode* node) {
5 if (!node) return 0;
6 long long s = node->val + subtree(node->left) + subtree(node->right);
7 sums.push_back(s);
8 return s;
9 }
10
11public:
12 int maxProduct(TreeNode* root) {
13 sums.clear();
14 long long total = subtree(root);
15 long long best = 0;
16 for (long long s : sums) {
17 best = max(best, s * (total - s));
18 }
19 return (int)(best % 1000000007LL);
20 }
21};Time and space complexity
O(n)
One DFS visits each node once and the scan touches each stored sum once.
O(n)
The list of subtree sums holds n values, plus O(h) recursion stack.
Other approaches
Two DFS passes without a list (time O(n), space O(h))
First DFS computes T. A second DFS recomputes each subtree sum and updates the best product on the fly. It saves the O(n) list at the cost of walking the tree twice.
Closest to half (time O(n), space O(n))
The product s * (T - s) grows as s approaches T / 2, so you can instead pick the stored sum closest to T / 2 and compute one product.
Common mistakes
- Taking the modulo before comparing. The largest remainder is not the remainder of the largest product, so compare raw products and reduce once at the end.
- Computing the product in 32-bit integers. With up to 5 * 10^4 nodes of value 10^4, T is up to 5 * 10^8 and the product overflows 32 bits.
- In JavaScript, plain numbers lose precision above 2^53, so use BigInt for the product.
- Keeping the stored sums in a field and not clearing it between calls, which mixes sums from different trees.
Frequently asked questions
Why is the root's own sum harmless in the scan?
For the root, s equals T, so the product is T * 0 = 0. Any real cut gives a positive product because every value is at least 1.
Why not take the modulo along the way?
The problem asks for the maximum product reduced at the end. Reducing early can make a smaller product look larger, so compare the exact 64-bit values.
Does the order of the stored sums matter?
No. The scan only needs every subtree sum once; post-order is just the natural order the DFS produces them in.