Skip to content

Commit b873701

Browse files
aQuaaQua
authored andcommitted
124 added
1 parent 77c97c7 commit b873701

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# [124. Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
2+
3+
## 题目
4+
Given a binary tree, find the maximum path sum.
5+
6+
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
7+
8+
For example:
9+
```
10+
Given the below binary tree,
11+
1
12+
/ \
13+
2 3
14+
15+
Return 6.
16+
```
17+
## 解题思路
18+
19+
见程序注释
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package Problem0124
2+
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Golang/kit"
5+
)
6+
type TreeNode = kit.TreeNode
7+
8+
func maxPathSum(root *TreeNode) int {
9+
10+
return 22
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Problem0124
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/aQuaYi/LeetCode-in-Golang/kit"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func Test_Problem0124(t *testing.T) {
13+
ast := assert.New(t)
14+
15+
// tcs is testcase slice
16+
tcs := []struct {
17+
pre, in []int
18+
ans int
19+
}{
20+
21+
{
22+
[]int{1, 2, 3},
23+
[]int{2, 1, 3},
24+
6,
25+
},
26+
27+
{
28+
[]int{4, 2, 1, 3, 6, 5, 7},
29+
[]int{1, 2, 3, 4, 5, 6, 7},
30+
22,
31+
},
32+
33+
// 可以多个 testcase
34+
}
35+
36+
for _, tc := range tcs {
37+
fmt.Printf("~~%v~~\n", tc)
38+
39+
root := kit.PreIn2Tree(tc.pre, tc.in)
40+
ast.Equal(tc.ans, maxPathSum(root), "输入:%v", tc)
41+
}
42+
}

0 commit comments

Comments
 (0)