-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path112_path_sum_test.go
53 lines (45 loc) · 1.11 KB
/
112_path_sum_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package _5_binary_tree
import (
"github.com/smartystreets/goconvey/convey"
"testing"
)
// 路径总和 https://leetcode.cn/problems/path-sum/description/
func TestHasPathSum(t *testing.T) {
convey.Convey("路径总和:是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum", t, func() {
testCase := []struct {
input *TreeNode
sum int
target bool
}{
{
Ints2TreeNode([]int{5, 4, 8, 11, NULL, 13, 4, 7, 2, NULL, NULL, NULL, 1}),
22,
true,
},
{
Ints2TreeNode([]int{1, 2, 3}),
5,
false,
},
{
Ints2TreeNode([]int{}),
0,
false,
},
}
for _, tst := range testCase {
rsp := hasPathSum(tst.input, tst.sum)
convey.So(rsp, convey.ShouldEqual, tst.target)
}
})
}
func hasPathSum(root *TreeNode, targetSum int) bool {
if root == nil {
return false // Since the tree is empty, there are no root-to-leaf paths
}
targetSum -= root.Val
if root.Left == nil && root.Right == nil { // 叶子节点
return targetSum == 0
}
return hasPathSum(root.Left, targetSum) || hasPathSum(root.Right, targetSum)
}