Skip to content

Commit c754950

Browse files
committed
feat: add solutions to lc problem: No.1339
No.1339.Maximum Product of Splitted Binary Tree
1 parent 63ede77 commit c754950

File tree

7 files changed

+255
-127
lines changed

7 files changed

+255
-127
lines changed

solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README.md

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
**方法一:DFS**
58+
**方法一:两次 DFS**
5959

60-
先通过 $sum$ 函数求得二叉树所有节点值的和,记为 $s$。然后 $DFS$ 求得以每个节点(除了根节点)作为子树根节点的所有节点值之和,记为 $t$,求得 $t \times (s - t)$ 的最大值,就是答案。注意取模操作。
60+
我们可以用两次 DFS 来解决这个问题。
61+
62+
第一次,我们用一个 $sum(root)$ 函数递归求出整棵树所有节点的和,记为 $s$。
63+
64+
第二次,我们用一个 $dfs(root)$ 函数递归遍历每个节点,求出以当前节点为根的子树的节点和 $t$,那么当前节点与其父节点分裂后两棵子树的节点和分别为 $t$ 和 $s - t$,它们的乘积为 $t \times (s - t)$,我们遍历所有节点,求出乘积的最大值,即为答案。
6165

6266
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
6367

@@ -76,25 +80,25 @@
7680
# self.right = right
7781
class Solution:
7882
def maxProduct(self, root: Optional[TreeNode]) -> int:
79-
def sum(root):
83+
def sum(root: Optional[TreeNode]) -> int:
8084
if root is None:
8185
return 0
8286
return root.val + sum(root.left) + sum(root.right)
8387

84-
def dfs(root):
85-
nonlocal s, ans
88+
def dfs(root: Optional[TreeNode]) -> int:
8689
if root is None:
8790
return 0
8891
t = root.val + dfs(root.left) + dfs(root.right)
92+
nonlocal ans, s
8993
if t < s:
9094
ans = max(ans, t * (s - t))
9195
return t
9296

97+
mod = 10**9 + 7
9398
s = sum(root)
9499
ans = 0
95100
dfs(root)
96-
ans %= (10**9 + 7)
97-
return ans
101+
return ans % mod
98102
```
99103

100104
### **Java**
@@ -120,20 +124,12 @@ class Solution:
120124
class Solution {
121125
private long ans;
122126
private long s;
123-
private static final int MOD = (int) 1e9 + 7;
124127

125128
public int maxProduct(TreeNode root) {
129+
final int mod = (int) 1e9 + 7;
126130
s = sum(root);
127131
dfs(root);
128-
ans %= MOD;
129-
return (int) ans;
130-
}
131-
132-
private long sum(TreeNode root) {
133-
if (root == null) {
134-
return 0;
135-
}
136-
return root.val + sum(root.left) + sum(root.right);
132+
return (int) (ans % mod);
137133
}
138134

139135
private long dfs(TreeNode root) {
@@ -146,6 +142,13 @@ class Solution {
146142
}
147143
return t;
148144
}
145+
146+
private long sum(TreeNode root) {
147+
if (root == null) {
148+
return 0;
149+
}
150+
return root.val + sum(root.left) + sum(root.right);
151+
}
149152
}
150153
```
151154

@@ -163,33 +166,35 @@ class Solution {
163166
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
164167
* };
165168
*/
166-
using ll = long long;
167-
const int MOD = 1e9 + 7;
168-
169169
class Solution {
170170
public:
171-
ll ans;
172-
ll s;
173-
174171
int maxProduct(TreeNode* root) {
175-
s = sum(root);
176-
dfs(root);
177-
ans %= MOD;
178-
return (int) ans;
179-
}
180-
181-
ll sum(TreeNode* root) {
182-
if (!root) return 0;
183-
return root->val + sum(root->left) + sum(root->right);
184-
}
172+
using ll = long long;
173+
ll ans = 0;
174+
const int mod = 1e9 + 7;
175+
176+
function<ll(TreeNode*)> sum = [&](TreeNode* root) -> ll {
177+
if (!root) {
178+
return 0;
179+
}
180+
return root->val + sum(root->left) + sum(root->right);
181+
};
182+
183+
ll s = sum(root);
184+
185+
function<ll(TreeNode*)> dfs = [&](TreeNode* root) -> ll {
186+
if (!root) {
187+
return 0;
188+
}
189+
ll t = root->val + dfs(root->left) + dfs(root->right);
190+
if (t < s) {
191+
ans = max(ans, t * (s - t));
192+
}
193+
return t;
194+
};
185195

186-
ll dfs(TreeNode* root) {
187-
if (!root) return 0;
188-
ll t = root->val + dfs(root->left) + dfs(root->right);
189-
if (t < s) {
190-
ans = max(ans, t * (s - t));
191-
}
192-
return t;
196+
dfs(root);
197+
return ans % mod;
193198
}
194199
};
195200
```
@@ -205,8 +210,8 @@ public:
205210
* Right *TreeNode
206211
* }
207212
*/
208-
func maxProduct(root *TreeNode) int {
209-
mod := int(1e9) + 7
213+
func maxProduct(root *TreeNode) (ans int) {
214+
const mod = 1e9 + 7
210215
var sum func(*TreeNode) int
211216
sum = func(root *TreeNode) int {
212217
if root == nil {
@@ -215,7 +220,6 @@ func maxProduct(root *TreeNode) int {
215220
return root.Val + sum(root.Left) + sum(root.Right)
216221
}
217222
s := sum(root)
218-
ans := 0
219223
var dfs func(*TreeNode) int
220224
dfs = func(root *TreeNode) int {
221225
if root == nil {
@@ -228,7 +232,8 @@ func maxProduct(root *TreeNode) int {
228232
return t
229233
}
230234
dfs(root)
231-
return ans % mod
235+
ans %= mod
236+
return
232237
}
233238

234239
func max(a, b int) int {
@@ -239,6 +244,48 @@ func max(a, b int) int {
239244
}
240245
```
241246

247+
### **TypeScript**
248+
249+
```ts
250+
/**
251+
* Definition for a binary tree node.
252+
* class TreeNode {
253+
* val: number
254+
* left: TreeNode | null
255+
* right: TreeNode | null
256+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
257+
* this.val = (val===undefined ? 0 : val)
258+
* this.left = (left===undefined ? null : left)
259+
* this.right = (right===undefined ? null : right)
260+
* }
261+
* }
262+
*/
263+
264+
function maxProduct(root: TreeNode | null): number {
265+
const sum = (root: TreeNode | null): number => {
266+
if (!root) {
267+
return 0;
268+
}
269+
return root.val + sum(root.left) + sum(root.right);
270+
};
271+
const s = sum(root);
272+
let ans = 0;
273+
const mod = 1e9 + 7;
274+
const dfs = (root: TreeNode | null): number => {
275+
if (!root) {
276+
return 0;
277+
}
278+
const t = root.val + dfs(root.left) + dfs(root.right);
279+
if (t < s) {
280+
ans = Math.max(ans, t * (s - t));
281+
}
282+
return t;
283+
};
284+
dfs(root);
285+
return ans % mod;
286+
}
287+
```
288+
242289
### **...**
243290

244291
```

0 commit comments

Comments
 (0)