-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path112.go
119 lines (103 loc) · 2.2 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
)
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
type TreeNodeSum struct {
Node *TreeNode
Sum int
}
func hasPathSum(root *TreeNode, sum int) bool {
var t *TreeNodeSum
if root == nil {
return false
}
rootSum := &TreeNodeSum{root, root.Val}
stack := make([]*TreeNodeSum, 0)
stack = append(stack, rootSum)
for len(stack) > 0 {
size := len(stack)
for i := 0; i < size; i++ {
p := stack[i]
if p.Node.Left != nil {
t = &TreeNodeSum{p.Node.Left, p.Sum + p.Node.Left.Val}
stack = append(stack, t)
}
if p.Node.Right != nil {
t = &TreeNodeSum{p.Node.Right, p.Sum + p.Node.Right.Val}
stack = append(stack, t)
}
//find the leaf and check the sum
if p.Node.Left == nil && p.Node.Right == nil {
if p.Sum == sum {
return true
}
}
}
stack = stack[size:]
}
return false
}
func MakeTreeNode(vals []int) *TreeNode {
var root, node0, node1, node2 *TreeNode
queue := make([]*TreeNode, 0)
i := 0
for {
if i == 0 {
if i >= len(vals) {
return root
}
node0 = &TreeNode{vals[i], nil, nil}
root = node0
queue = append(queue, node0)
i++
continue
}
node0Num := len(queue)
for j := 0; j < node0Num; j++ {
node0 = queue[j]
if i >= len(vals) {
return root
}
if node0 == nil {
//no children parse
continue
}
if vals[i] == -1 {
node1 = nil
} else {
node1 = &TreeNode{vals[i], nil, nil}
}
node0.Left = node1
queue = append(queue, node1)
if vals[i+1] == -1 {
node2 = nil
} else {
node2 = &TreeNode{vals[i+1], nil, nil}
}
node0.Right = node2
queue = append(queue, node2)
i += 2
}
queue = queue[node0Num:]
}
return root
}
func main() {
root := MakeTreeNode([]int{1, 2, 2, 3, 4, 4, 3})
fmt.Println(hasPathSum(root, 7))
root = MakeTreeNode([]int{1, 2, 2, -1, 3, -1, 3})
fmt.Println(hasPathSum(root, 6))
root = MakeTreeNode([]int{1, 2, 2, -1, -1, -1, 3})
fmt.Println(hasPathSum(root, 3))
root = MakeTreeNode([]int{1})
fmt.Println(hasPathSum(root, 1))
root = MakeTreeNode([]int{})
fmt.Println(hasPathSum(root, 1))
root = MakeTreeNode([]int{9, -42, -42, -1, 76, 76, -1, -1, 13, -1, 13})
fmt.Println(hasPathSum(root, 3))
}