Skip to content

Commit e2f489c

Browse files
committed
day 19
1 parent 29e8a2a commit e2f489c

File tree

2 files changed

+102
-20
lines changed

2 files changed

+102
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Maximum Product of Splitted Binary Tree
3+
=======================================
4+
5+
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.
6+
7+
Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo 109 + 7.
8+
9+
Note that you need to maximize the answer before taking the mod and not after taking it.
10+
11+
Example 1:
12+
Input: root = [1,2,3,4,5,6]
13+
Output: 110
14+
Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
15+
16+
Example 2:
17+
Input: root = [1,null,2,3,4,null,null,5,6]
18+
Output: 90
19+
Explanation: Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
20+
21+
Example 3:
22+
Input: root = [2,3,9,10,7,8,6,5,4,11,1]
23+
Output: 1025
24+
25+
Example 4:
26+
Input: root = [1,1]
27+
Output: 1
28+
29+
Constraints:
30+
The number of nodes in the tree is in the range [2, 5 * 104].
31+
1 <= Node.val <= 104
32+
*/
33+
34+
/**
35+
* Definition for a binary tree node.
36+
* struct TreeNode {
37+
* int val;
38+
* TreeNode *left;
39+
* TreeNode *right;
40+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
41+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
42+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
43+
* };
44+
*/
45+
46+
#define ll long long
47+
int M = 1e9 + 7;
48+
49+
class Solution
50+
{
51+
public:
52+
int dfs1(TreeNode *root)
53+
{
54+
if (!root)
55+
return 0;
56+
return root->val + dfs1(root->left) + dfs1(root->right);
57+
}
58+
59+
int dfs2(TreeNode *root, ll &ans, ll totalSum)
60+
{
61+
int sumHere = root->val;
62+
63+
if (root->left)
64+
sumHere += dfs2(root->left, ans, totalSum);
65+
if (root->right)
66+
sumHere += dfs2(root->right, ans, totalSum);
67+
68+
ans = max(ans, sumHere * (totalSum - sumHere));
69+
return sumHere;
70+
}
71+
72+
int maxProduct(TreeNode *root)
73+
{
74+
ll fullSum = dfs1(root);
75+
ll ans = 0;
76+
77+
dfs2(root, ans, fullSum);
78+
79+
return ans % M;
80+
}
81+
};
+21-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
# August 2021 LeetCoding Challenge
22

3-
| Day | Question Links | Solutions |
4-
| :-: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------: |
5-
| | [Optimize Water Distribution in a Village](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3834/) | [cpp](./Optimize%20Water%20Distribution%20in%20a%20Village.cpp) |
6-
| 1. | [Making A Large Island](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3835/) | [cpp](./01.%20Making%20A%20Large%20Island.cpp) |
7-
| 2. | [Two Sum](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3836/) | [cpp](./02.%20Two%20Sum.cpp) |
8-
| 3. | [Subsets II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |
9-
| 4. | [Path Sum II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3838/) | [cpp](./04.%20Path%20Sum%20II.cpp) |
10-
| 5. | [Stone Game](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3870/) | [cpp](./05.%20Stone%20Game.cpp) |
11-
| 6. | [N-ary Tree Level Order Traversal](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3871/) | [cpp](./06.%20N-ary%20Tree%20Level%20Order%20Traversal.cpp) |
12-
| 7. | [Palindrome Partitioning II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3872/) | [cpp](./07.%20Palindrome%20Partitioning%20II.cpp) |
13-
| | [Paint Fence](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3873/) | [cpp](./Paint%20Fence.cpp) |
14-
| 9. | [Add Strings](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3875/) | [cpp](./09.%20Add%20Strings.cpp) |
15-
| 10. | [Flip String to Monotone Increasing](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3876/) | [cpp](./10.%20Flip%20String%20to%20Monotone%20Increasing.cpp) |
16-
| 11. | [Array of Doubled Pairs](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3877/) | [cpp](./11.%20Array%20of%20Doubled%20Pairs.cpp) |
17-
| 12. | [Group Anagrams](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3887/) | [cpp](./12.%20Group%20Anagrams.cpp) |
18-
| 13. | [Set Matrix Zeroes](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3888/) | [cpp](./13.%20Set%20Matrix%20Zeroes.cpp) |
19-
| 15. | [Minimum Window Substring](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3891/) | [cpp](./15.%20Minimum%20Window%20Substring.cpp) |
20-
| 16. | [Range Sum Query - Immutable](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3892/) | [cpp](./16.%20Range%20Sum%20Query%20-%20Immutable.cpp) |
21-
| 17. | [Count Good Nodes in Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3899/) | [cpp](./17.%20Count%20Good%20Nodes%20in%20Binary%20Tree.cpp) |
22-
| 18. | [Decode Ways](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3902/) | [cpp](./18.%20Decode%20Ways.cpp) |
3+
| Day | Question Links | Solutions |
4+
| :-: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------: |
5+
| | [Optimize Water Distribution in a Village](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3834/) | [cpp](./Optimize%20Water%20Distribution%20in%20a%20Village.cpp) |
6+
| 1. | [Making A Large Island](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3835/) | [cpp](./01.%20Making%20A%20Large%20Island.cpp) |
7+
| 2. | [Two Sum](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3836/) | [cpp](./02.%20Two%20Sum.cpp) |
8+
| 3. | [Subsets II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |
9+
| 4. | [Path Sum II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3838/) | [cpp](./04.%20Path%20Sum%20II.cpp) |
10+
| 5. | [Stone Game](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3870/) | [cpp](./05.%20Stone%20Game.cpp) |
11+
| 6. | [N-ary Tree Level Order Traversal](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3871/) | [cpp](./06.%20N-ary%20Tree%20Level%20Order%20Traversal.cpp) |
12+
| 7. | [Palindrome Partitioning II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3872/) | [cpp](./07.%20Palindrome%20Partitioning%20II.cpp) |
13+
| | [Paint Fence](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3873/) | [cpp](./Paint%20Fence.cpp) |
14+
| 9. | [Add Strings](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3875/) | [cpp](./09.%20Add%20Strings.cpp) |
15+
| 10. | [Flip String to Monotone Increasing](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3876/) | [cpp](./10.%20Flip%20String%20to%20Monotone%20Increasing.cpp) |
16+
| 11. | [Array of Doubled Pairs](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3877/) | [cpp](./11.%20Array%20of%20Doubled%20Pairs.cpp) |
17+
| 12. | [Group Anagrams](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3887/) | [cpp](./12.%20Group%20Anagrams.cpp) |
18+
| 13. | [Set Matrix Zeroes](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/614/week-2-august-8th-august-14th/3888/) | [cpp](./13.%20Set%20Matrix%20Zeroes.cpp) |
19+
| 15. | [Minimum Window Substring](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3891/) | [cpp](./15.%20Minimum%20Window%20Substring.cpp) |
20+
| 16. | [Range Sum Query - Immutable](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3892/) | [cpp](./16.%20Range%20Sum%20Query%20-%20Immutable.cpp) |
21+
| 17. | [Count Good Nodes in Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3899/) | [cpp](./17.%20Count%20Good%20Nodes%20in%20Binary%20Tree.cpp) |
22+
| 18. | [Decode Ways](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3902/) | [cpp](./18.%20Decode%20Ways.cpp) |
23+
| 19. | [Maximum Product of Splitted Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3903/) | [cpp](./19.%20Maximum%20Product%20of%20Splitted%20Binary%20Tree.cpp) |

0 commit comments

Comments
 (0)