-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path112.go
51 lines (45 loc) · 1.04 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
package main
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func hasPathSum(root *TreeNode, sum int) bool {
if root == nil {
return false
}
type NodeSum struct {
Node *TreeNode
Sum int
}
nodes := []NodeSum{{root, 0}}
for len(nodes) != 0 {
node := nodes[len(nodes) - 1]
nodes = nodes[:len(nodes) - 1]
if node.Node.Val + node.Sum == sum && node.Node.Left == nil && node.Node.Right == nil {
return true
}
if node.Node.Left != nil {
nodes = append(nodes, NodeSum{node.Node.Left, node.Sum + node.Node.Val})
}
if node.Node.Right != nil {
nodes = append(nodes, NodeSum{node.Node.Right, node.Sum + node.Node.Val})
}
}
return false
}
if not root:
return False
nodes = [(0, root)]
while nodes:
parent_num, node = nodes.pop()
if not node:
continue
if (cur_num := node.val + parent_num) == sum and not node.left and not node.right:
return True
if node.left:
nodes.append((cur_num, node.left))
if node.right:
nodes.append((cur_num, node.right))
return False