-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path112.路径总和.go
96 lines (87 loc) · 1.72 KB
/
112.路径总和.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* @lc app=leetcode.cn id=112 lang=golang
*
* [112] 路径总和
*/
// @lc code=start
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
package main
import "fmt"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func hasPathSum(root *TreeNode, sum int) bool {
// return true
if root == nil {
return false
}
if root.Left == nil && root.Right == nil {
return root.Val == sum
}
return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val)
}
// @lc code=end
func print(root *TreeNode){
// fmt.Printf("%d ", root.Val)
// if root.Left != nil {
// print(root.Left)
// }
// if root.Right != nil {
// print(root.Right)
// }
if root == nil {
return
}
stack := []*TreeNode{}
stack = append(stack, root)
for len(stack) > 0 {
size := len(stack)
for i := 0 ; i < size ; i++ {
tmp := stack[0]
stack = stack[1:]
fmt.Printf("%d ", tmp.Val)
if tmp.Left != nil {
stack = append(stack, tmp.Left)
}
if tmp.Right != nil {
stack = append(stack, tmp.Right)
}
}
fmt.Println()
}
}
func main(){
t11 := TreeNode{Val: 5}
t21 := TreeNode{Val: 4}
t22 := TreeNode{Val: 8}
t31 := TreeNode{Val: 11}
// t32 := TreeNode{Val: 13}
t33 := TreeNode{Val: 13}
t34 := TreeNode{Val: 4}
t41 := TreeNode{Val: 7}
t42 := TreeNode{Val: 2}
t43 := TreeNode{Val: 1}
t11.Left = &t21
t11.Right = &t22
t21.Left = &t31
// t21.Right = &t32
t22.Left = &t33
t22.Right = &t34
t31.Left = &t41
t31.Right = &t42
t34.Right = &t43
print(&t11)
fmt.Println(hasPathSum(&t11, 22))
fmt.Println(hasPathSum(&t11, 18))
fmt.Println(hasPathSum(&t11, 26))
fmt.Println(hasPathSum(&t11, 25))
}