-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1339.cpp
More file actions
34 lines (31 loc) · 973 Bytes
/
Copy path1339.cpp
File metadata and controls
34 lines (31 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Definition for a binary tree node.
// struct TreeNode {
// int val;
// TreeNode *left;
// TreeNode *right;
// TreeNode() : val(0), left(nullptr), right(nullptr) {}
// TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
// TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
// right(right) {}
// };
class Solution {
long long totalSum = 0, result = 0;
const static int MOD = 1e9 + 7;
public:
int maxProduct(TreeNode* root) {
totalSum = get_sum(root);
buttom_up_update_result(root);
return result % MOD;
}
int get_sum(TreeNode* root) {
if (!root) return 0;
return root->val + get_sum(root->left) + get_sum(root->right);
}
int buttom_up_update_result(TreeNode* root) {
if (!root) return 0;
int tmp_sum = root->val + buttom_up_update_result(root->left) +
buttom_up_update_result(root->right);
result = max(result, tmp_sum * (totalSum - tmp_sum));
return tmp_sum;
}
};