Skip to content

Commit 3f86760

Browse files
chore: add LeetCode daily solution
1 parent 79cf727 commit 3f86760

5 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Maximum Product of Splitted Binary Tree (Medium)
2+
3+
**Problem ID:** 1339
4+
**Date:** 2026-01-07
5+
**Link:** https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/
6+
7+
## Approach
8+
9+
To solve the problem of maximizing the product of the sums of two subtrees obtained by splitting a binary tree, we can follow a systematic approach:
10+
11+
### Problem Breakdown
12+
1. **Understanding the Tree Structure**: We need to consider how splitting the tree at various edges affects the sums of the resulting subtrees. Each edge can be thought of as a potential split point.
13+
14+
2. **Sum Calculation**: The first step is to calculate the total sum of all node values in the tree. This will help us determine the sum of one subtree once we know the sum of the other subtree (since the total sum is constant).
15+
16+
3. **DFS for Subtree Sums**: We can perform a Depth-First Search (DFS) traversal to calculate the sum of each subtree rooted at every node. During this traversal:
17+
- For each node, compute the sum of its subtree.
18+
- Store the subtree sums in a list or variable.
19+
20+
4. **Maximizing the Product**: As we compute the subtree sums, we can calculate the product of the sum of the current subtree with the sum of the remaining tree (which is the total sum minus the current subtree sum). Specifically, for a subtree sum `subtree_sum`, the product can be calculated as:
21+
\[
22+
\text{product} = \text{subtree\_sum} \times (\text{total\_sum} - \text{subtree\_sum})
23+
\]
24+
We need to keep track of the maximum product encountered during this process.
25+
26+
5. **Modulo Operation**: Since the result can be very large, we will take the product modulo \(10^9 + 7\) at the end.
27+
28+
### Data Structures
29+
- A binary tree structure to represent the nodes.
30+
- Variables to store the total sum of the tree and the maximum product found.
31+
32+
### Complexity
33+
- **Time Complexity**: The algorithm primarily consists of a single DFS traversal of the tree, which takes \(O(N)\) time, where \(N\) is the number of nodes in the tree.
34+
- **Space Complexity**: The space complexity is \(O(H)\) for the recursion stack, where \(H\) is the height of the tree. In the worst case (for a skewed tree), this could be \(O(N)\), but for a balanced tree, it would be \(O(\log N)\).
35+
36+
### Summary
37+
By leveraging a DFS to calculate subtree sums and simultaneously compute the maximum product of the sums of the two resulting subtrees, we can efficiently solve the problem. The key is to maintain the total sum and use it to derive the product for each potential split point, ensuring we take the result modulo \(10^9 + 7\) before returning it.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
private long totalSum = 0;
3+
private long maxProduct = 0;
4+
private final int MOD = 1000000007;
5+
6+
public int maxProduct(TreeNode root) {
7+
totalSum = calculateSum(root);
8+
calculateMaxProduct(root);
9+
return (int) (maxProduct % MOD);
10+
}
11+
12+
private long calculateSum(TreeNode node) {
13+
if (node == null) return 0;
14+
long leftSum = calculateSum(node.left);
15+
long rightSum = calculateSum(node.right);
16+
return node.val + leftSum + rightSum;
17+
}
18+
19+
private long calculateMaxProduct(TreeNode node) {
20+
if (node == null) return 0;
21+
long leftSum = calculateMaxProduct(node.left);
22+
long rightSum = calculateMaxProduct(node.right);
23+
long currentSum = node.val + leftSum + rightSum;
24+
25+
if (leftSum > 0) {
26+
maxProduct = Math.max(maxProduct, leftSum * (totalSum - leftSum));
27+
}
28+
if (rightSum > 0) {
29+
maxProduct = Math.max(maxProduct, rightSum * (totalSum - rightSum));
30+
}
31+
return currentSum;
32+
}
33+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val === undefined ? 0 : val);
5+
* this.left = (left === undefined ? null : left);
6+
* this.right = (right === undefined ? null : right);
7+
* }
8+
*/
9+
10+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
var maxProduct = function(root) {
15+
const MOD = 1e9 + 7;
16+
let totalSum = 0;
17+
let maxProd = 0;
18+
19+
const calculateSum = (node) => {
20+
if (!node) return 0;
21+
const leftSum = calculateSum(node.left);
22+
const rightSum = calculateSum(node.right);
23+
const currentSum = leftSum + rightSum + node.val;
24+
totalSum += currentSum;
25+
return currentSum;
26+
};
27+
28+
const findMaxProduct = (node) => {
29+
if (!node) return 0;
30+
const leftSum = findMaxProduct(node.left);
31+
const rightSum = findMaxProduct(node.right);
32+
const currentSum = leftSum + rightSum + node.val;
33+
const otherSum = totalSum - currentSum;
34+
maxProd = Math.max(maxProd, currentSum * otherSum);
35+
return currentSum;
36+
};
37+
38+
calculateSum(root);
39+
findMaxProduct(root);
40+
41+
return maxProd % MOD;
42+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class TreeNode:
2+
def __init__(self, val=0, left=None, right=None):
3+
self.val = val
4+
self.left = left
5+
self.right = right
6+
7+
class Solution:
8+
def maxProduct(self, root: TreeNode) -> int:
9+
MOD = 10**9 + 7
10+
11+
def total_sum(node):
12+
if not node:
13+
return 0
14+
return node.val + total_sum(node.left) + total_sum(node.right)
15+
16+
total = total_sum(root)
17+
max_product = 0
18+
19+
def find_max_product(node):
20+
nonlocal max_product
21+
if not node:
22+
return 0
23+
left_sum = find_max_product(node.left)
24+
right_sum = find_max_product(node.right)
25+
current_sum = node.val + left_sum + right_sum
26+
27+
if left_sum > 0:
28+
max_product = max(max_product, left_sum * (total - left_sum))
29+
if right_sum > 0:
30+
max_product = max(max_product, right_sum * (total - right_sum))
31+
32+
return current_sum
33+
34+
find_max_product(root)
35+
return max_product % MOD

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
328328
- 2026-01-04 — [Four Divisors](https://leetcode.com/problems/four-divisors/) (Medium) → `Medium/2026-01-04-1390-Four-Divisors`
329329
- 2026-01-05 — [Maximum Matrix Sum](https://leetcode.com/problems/maximum-matrix-sum/) (Medium) → `Medium/2026-01-05-1975-Maximum-Matrix-Sum`
330330
- 2026-01-06 — [Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) (Medium) → `Medium/2026-01-06-1161-Maximum-Level-Sum-of-a-Binary-Tree`
331+
- 2026-01-07 — [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) (Medium) → `Medium/2026-01-07-1339-Maximum-Product-of-Splitted-Binary-Tree`

0 commit comments

Comments
 (0)