题目
Given the root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.
Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo 109 + 7.
Note that you need to maximize the answer before taking the mod and not after taking it.
解题思路
先求出整个树的和s
,再使用dfs遍历整个树,求出每个子树的和t
,用t*(s-t)
更新结果
代码
class Solution:
def maxProduct(self, root: Optional[TreeNode]) -> int:
def dfs(root):
if not root:
return 0
left, right = dfs(root.left), dfs(root.right)
res[0] = max(res[0], left * (s[0] - left), right * (s[0] - right))
return left + right + root.val
s = 0
q = [root]
while q:
cur = q.pop()
s += cur.val
if cur.left:
q.append(cur.left)
if cur.right:
q.append(cur.right)
s = [s]
res = [0]
dfs(root)
return res[0] % (10**9 + 7)