55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
- ** 方法一:DFS**
58
+ ** 方法一:两次 DFS**
59
59
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)$,我们遍历所有节点,求出乘积的最大值,即为答案。
61
65
62
66
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
63
67
76
80
# self.right = right
77
81
class Solution :
78
82
def maxProduct (self , root : Optional[TreeNode]) -> int :
79
- def sum (root ) :
83
+ def sum (root : Optional[TreeNode]) -> int :
80
84
if root is None :
81
85
return 0
82
86
return root.val + sum (root.left) + sum (root.right)
83
87
84
- def dfs (root ):
85
- nonlocal s, ans
88
+ def dfs (root : Optional[TreeNode]) -> int :
86
89
if root is None :
87
90
return 0
88
91
t = root.val + dfs(root.left) + dfs(root.right)
92
+ nonlocal ans, s
89
93
if t < s:
90
94
ans = max (ans, t * (s - t))
91
95
return t
92
96
97
+ mod = 10 ** 9 + 7
93
98
s = sum (root)
94
99
ans = 0
95
100
dfs(root)
96
- ans %= (10 ** 9 + 7 )
97
- return ans
101
+ return ans % mod
98
102
```
99
103
100
104
### ** Java**
@@ -120,20 +124,12 @@ class Solution:
120
124
class Solution {
121
125
private long ans;
122
126
private long s;
123
- private static final int MOD = (int ) 1e9 + 7 ;
124
127
125
128
public int maxProduct (TreeNode root ) {
129
+ final int mod = (int ) 1e9 + 7 ;
126
130
s = sum(root);
127
131
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);
137
133
}
138
134
139
135
private long dfs (TreeNode root ) {
@@ -146,6 +142,13 @@ class Solution {
146
142
}
147
143
return t;
148
144
}
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
+ }
149
152
}
150
153
```
151
154
@@ -163,33 +166,35 @@ class Solution {
163
166
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
164
167
* };
165
168
*/
166
- using ll = long long ;
167
- const int MOD = 1e9 + 7 ;
168
-
169
169
class Solution {
170
170
public:
171
- ll ans;
172
- ll s;
173
-
174
171
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
+ };
185
195
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;
193
198
}
194
199
};
195
200
```
@@ -205,8 +210,8 @@ public:
205
210
* Right *TreeNode
206
211
* }
207
212
*/
208
- func maxProduct(root *TreeNode) int {
209
- mod := int( 1e9) + 7
213
+ func maxProduct (root *TreeNode ) ( ans int ) {
214
+ const mod = 1e9 + 7
210
215
var sum func (*TreeNode) int
211
216
sum = func (root *TreeNode) int {
212
217
if root == nil {
@@ -215,7 +220,6 @@ func maxProduct(root *TreeNode) int {
215
220
return root.Val + sum (root.Left ) + sum (root.Right )
216
221
}
217
222
s := sum (root)
218
- ans := 0
219
223
var dfs func (*TreeNode) int
220
224
dfs = func (root *TreeNode) int {
221
225
if root == nil {
@@ -228,7 +232,8 @@ func maxProduct(root *TreeNode) int {
228
232
return t
229
233
}
230
234
dfs (root)
231
- return ans % mod
235
+ ans %= mod
236
+ return
232
237
}
233
238
234
239
func max (a , b int ) int {
@@ -239,6 +244,48 @@ func max(a, b int) int {
239
244
}
240
245
```
241
246
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
+
242
289
### ** ...**
243
290
244
291
```
0 commit comments