-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 370 ms (10.39%), Space: 78.1 MB (45.83%) - LeetHub
- Loading branch information
1 parent
a03efc8
commit f172711
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...-maximum-product-of-splitted-binary-tree/1339-maximum-product-of-splitted-binary-tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution { | ||
public: | ||
#define mod 1000000007 | ||
long long tsum; | ||
long long getSum(TreeNode* root) | ||
{ | ||
if(root==NULL)return 0; | ||
return root->val+(getSum(root->left)+getSum(root->right)); | ||
} | ||
long long solve(TreeNode* root,long long &mx) | ||
{ | ||
if(root==NULL)return 0; | ||
long long sum1=root->val+solve(root->left,mx)+solve(root->right,mx); | ||
long long ext=(sum1); | ||
long long ent=(tsum-sum1); | ||
long long ans=(ext*ent); | ||
mx= max(ans,mx); | ||
return sum1; | ||
|
||
} | ||
int maxProduct(TreeNode* root) { | ||
tsum=getSum(root); | ||
cout<<tsum; | ||
long long mx=LONG_MIN; | ||
solve(root,mx); | ||
return mx%mod; | ||
} | ||
}; |