-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
46 lines (42 loc) · 690 Bytes
/
main.go
File metadata and controls
46 lines (42 loc) · 690 Bytes
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
package main
import (
"log"
)
// Tree Node
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/*
*
*
* has Path Sum
*
* args :
* root *TreeNode
* targetSum int
*
*
* return value:
* bool
*
*
*/
func hasPathSum(root *TreeNode, targetSum int) bool {
if root == nil {
return false
}
targetSum -= root.Val
if targetSum == 0 && root.Left == nil && root.Right == nil {
return true
}
return hasPathSum(root.Left, targetSum) || hasPathSum(root.Right, targetSum)
}
func main() {
// simple test
if ans := hasPathSum(&TreeNode{1, nil, nil}, 1); !ans {
log.Fatalf("Worn Answer! answer equal `True` not equal : %v", ans)
}
log.Println("Answer is True!")
}