Skip to content

Commit c5aa445

Browse files
committed
测试
1 parent 61edb01 commit c5aa445

File tree

5 files changed

+76
-51
lines changed

5 files changed

+76
-51
lines changed

er-cha-shu-ran-se-UGC/export.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package er_cha_shu_ran_se_ugc
2-
func MaxValue(root *TreeNode, k int) int {
3-
return maxValue(root,k)
4-
}
1+
package er_cha_shu_ran_se_ugc
2+
3+
func MaxValue(root *TreeNode, k int) int {
4+
return maxValue(root, k)
5+
}

er-cha-shu-ran-se-UGC/index.go

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
package er_cha_shu_ran_se_ugc
2-
3-
import (
4-
"math"
5-
6-
sadbt "github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree"
7-
)
8-
9-
type TreeNode = sadbt.TreeNode
10-
11-
func maxValue(root *TreeNode, k int) int {
12-
return Max(dfs(root, k)...)
13-
}
14-
15-
func dfs(root *TreeNode, k int) []int {
16-
ans := make([]int, k+1)
17-
if root == nil {
18-
return ans
19-
}
20-
21-
left := dfs(root.Left, k)
22-
right := dfs(root.Right, k)
23-
for i := range ans {
24-
ans[i] = left[k] + right[k]
25-
}
26-
27-
for i := 1; i <= k; i++ {
28-
temp := math.MinInt
29-
for j := 0; j <= i-1; j++ {
30-
temp = Max(temp, left[j]+right[i-1-j])
31-
}
32-
ans[i] = Max(ans[i], temp+root.Val)
33-
}
34-
return ans
35-
}
36-
func Max(values ...int) int {
37-
38-
value := math.MinInt
39-
40-
for _, v := range values {
41-
if value < v {
42-
value = v
43-
}
44-
}
45-
return value
46-
}
1+
package er_cha_shu_ran_se_ugc
2+
3+
import (
4+
"math"
5+
6+
sadbt "github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree"
7+
)
8+
9+
type TreeNode = sadbt.TreeNode
10+
11+
func maxValue(root *TreeNode, k int) int {
12+
return Max(dfs(root, k)...)
13+
}
14+
15+
func dfs(root *TreeNode, k int) []int {
16+
ans := make([]int, k+1)
17+
if root == nil {
18+
return ans
19+
}
20+
21+
left := dfs(root.Left, k)
22+
right := dfs(root.Right, k)
23+
for i := range ans {
24+
ans[i] = left[k] + right[k]
25+
}
26+
27+
for i := 1; i <= k; i++ {
28+
temp := math.MinInt
29+
for j := 0; j <= i-1; j++ {
30+
temp = Max(temp, left[j]+right[i-1-j])
31+
}
32+
ans[i] = Max(ans[i], temp+root.Val)
33+
}
34+
return ans
35+
}
36+
func Max(values ...int) int {
37+
38+
value := math.MinInt
39+
40+
for _, v := range values {
41+
if value < v {
42+
value = v
43+
}
44+
}
45+
return value
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package er_cha_shu_ran_se_ugc
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
import "github.com/masx200/leetcode-test/utils"
9+
10+
var TreeNodeLeetCodeParse = utils.TreeNodeLeetCodeParse
11+
12+
func TestMaxValue(t *testing.T) {
13+
var assertEquals = func(a, b any) {
14+
assert.Equal(t, a, b)
15+
}
16+
assertEquals(12, maxValue(TreeNodeLeetCodeParse("[5, 2, 3, 4]"), 2))
17+
assertEquals(
18+
16,
19+
maxValue(TreeNodeLeetCodeParse("[4, 1, 3, 9, null, null, 2]"), 2),
20+
)
21+
}

er-cha-shu-ran-se-UGC/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Deno.test("er-cha-shu-ran-se-UGC", () => {
55
assertEquals(12, maxValue(TreeNodeLeetCodeFromJSON([5, 2, 3, 4]), 2));
66
assertEquals(
77
16,
8-
maxValue(TreeNodeLeetCodeFromJSON([4, 1, 3, 9, null, null, 2]), 2)
8+
maxValue(TreeNodeLeetCodeFromJSON([4, 1, 3, 9, null, null, 2]), 2),
99
);
1010
});

utils/TreeNodeLeetCodeParse.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package utils
22

33
import (
4+
"strings"
5+
46
treenode "github.com/masx200/leetcode-TreeNode-go"
57
serialize_and_deserialize_binary_tree "github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree"
68
)
79

810
type TreeNode = serialize_and_deserialize_binary_tree.TreeNode
911

1012
func TreeNodeLeetCodeParse(s string) *TreeNode {
13+
s = strings.ReplaceAll(s, " ", "")
1114
var t, e = treenode.NewTreeNode(s)
1215
if e != nil {
1316
panic(e)

0 commit comments

Comments
 (0)