Skip to content

Commit 05a3ab8

Browse files
committed
Merge pull request #371 from 0xff-dev/master
Add solution and test-cases for problem 1339
2 parents ce78566 + ddfdf07 commit 05a3ab8

File tree

5 files changed

+95
-24
lines changed

5 files changed

+95
-24
lines changed

leetcode/1301-1400/1339.Maximum-Product-of-Splitted-Binary-Tree/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [1339.Maximum Product of Splitted Binary Tree][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it **modulo** 10<sup>9</sup> + 7.
7+
8+
**Note** that you need to maximize the answer before taking the mod and not after taking it.
79

8-
**Example 1:**
10+
**Example 1:**
11+
![example1](./sample_1_1699.png)
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: root = [1,2,3,4,5,6]
15+
Output: 110
16+
Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
20+
![example2](./sample_2_1699.png)
1721

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Product of Splitted Binary Tree
23-
```go
2422
```
25-
23+
Input: root = [1,null,2,3,4,null,null,5,6]
24+
Output: 90
25+
Explanation: Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
26+
```
2627

2728
## 结语
2829

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left, Right *TreeNode
6+
}
7+
8+
const mod1339 = 1000000007
9+
10+
func Solution(root *TreeNode) int {
11+
sum := treeSum(root)
12+
ans := -1
13+
splitTree(root, sum, &ans)
14+
return ans % mod1339
15+
}
16+
17+
func treeSum(root *TreeNode) int {
18+
if root == nil {
19+
return 0
20+
}
21+
22+
left := treeSum(root.Left)
23+
right := treeSum(root.Right)
24+
root.Val += left + right
25+
return root.Val
26+
}
27+
28+
func splitTree(root *TreeNode, sum int, ans *int) {
29+
if root == nil {
30+
return
31+
}
32+
if root.Left != nil {
33+
splitTree(root.Left, sum, ans)
34+
}
35+
36+
if root.Right != nil {
37+
splitTree(root.Right, sum, ans)
38+
}
39+
diff := sum - root.Val
40+
s := diff * root.Val
41+
if *ans == -1 || s > *ans {
42+
*ans = s
43+
}
544
}

leetcode/1301-1400/1339.Maximum-Product-of-Splitted-Binary-Tree/Solution_test.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,43 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &TreeNode{
17+
Val: 1,
18+
Left: &TreeNode{
19+
Val: 2,
20+
Left: &TreeNode{
21+
Val: 4,
22+
},
23+
Right: &TreeNode{
24+
Val: 5,
25+
},
26+
},
27+
Right: &TreeNode{
28+
Val: 3,
29+
Left: &TreeNode{Val: 6},
30+
},
31+
}, 110},
32+
{"TestCase2", &TreeNode{
33+
Val: 1,
34+
Right: &TreeNode{
35+
Val: 2,
36+
Left: &TreeNode{
37+
Val: 3,
38+
},
39+
Right: &TreeNode{
40+
Val: 4,
41+
Left: &TreeNode{
42+
Val: 5,
43+
},
44+
Right: &TreeNode{
45+
Val: 6,
46+
},
47+
},
48+
},
49+
}, 90},
1950
}
2051

2152
// 开始测试
@@ -30,10 +61,10 @@ func TestSolution(t *testing.T) {
3061
}
3162
}
3263

33-
// 压力测试
64+
// 压力测试
3465
func BenchmarkSolution(b *testing.B) {
3566
}
3667

37-
// 使用案列
68+
// 使用案列
3869
func ExampleSolution() {
3970
}
Loading
Loading

0 commit comments

Comments
 (0)