-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path112-PathSum.go
More file actions
143 lines (131 loc) · 4.09 KB
/
112-PathSum.go
File metadata and controls
143 lines (131 loc) · 4.09 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
// 112. Path Sum
// Given the root of a binary tree and an integer targetSum,
// return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.s
// A leaf is a node with no children.
// Example 1:
// [5]
// / \
// [4] 8
// / / \
// [11] 13 4
// / \ \
// 7 [2] 1
// <img src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" />
// Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
// Output: true
// Explanation: The root-to-leaf path with the target sum is shown.
// Example 2:
// 1
// / \
// 2 3
// <img src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" />
// Input: root = [1,2,3], targetSum = 5
// Output: false
// Explanation: There two root-to-leaf paths in the tree:
// (1 --> 2): The sum is 3.
// (1 --> 3): The sum is 4.
// There is no root-to-leaf path with sum = 5.
// Example 3:
// Input: root = [], targetSum = 0
// Output: false
// Explanation: Since the tree is empty, there are no root-to-leaf paths.
// Constraints:
// The number of nodes in the tree is in the range [0, 5000].
// -1000 <= Node.val <= 1000
// -1000 <= targetSum <= 1000
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 递归
func hasPathSum(root *TreeNode, targetSum int) bool {
if root == nil {
return false
}
if root.Left == nil && root.Right == nil {
return targetSum == root.Val
}
return hasPathSum(root.Left, targetSum - root.Val) || hasPathSum(root.Right, targetSum - root.Val)
}
// dfs
func hasPathSum1(root *TreeNode, targetSum int) bool {
res := false
if root == nil {
return res
}
var dfs func(root *TreeNode, targetSum, currentSum int)
dfs = func(root *TreeNode, targetSum, currentSum int) {
if root.Left == nil && root.Right == nil {
if targetSum == currentSum+root.Val {
res = true
}
return
}
currentSum += root.Val
if root.Left != nil {
dfs(root.Left, targetSum, currentSum)
}
if root.Right != nil {
dfs(root.Right, targetSum, currentSum)
}
}
dfs(root, targetSum, 0)
return res
}
func main() {
// Example 1:
// [5]
// / \
// [4] 8
// / / \
// [11] 13 4
// / \ \
// 7 [2] 1
// <img src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" />
// Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
// Output: true
// Explanation: The root-to-leaf path with the target sum is shown.
tree1 := &TreeNode {
5,
&TreeNode{4, &TreeNode{11, &TreeNode{7, nil, nil}, &TreeNode{2, nil, nil}, }, nil},
&TreeNode{8, &TreeNode{13, nil, nil}, &TreeNode{4, nil, &TreeNode{1, nil, nil}, }, },
}
fmt.Println(hasPathSum(tree1,22)) // true
// Example 2:
// 1
// / \
// 2 3
// <img src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" />
// Input: root = [1,2,3], targetSum = 5
// Output: false
// Explanation: There two root-to-leaf paths in the tree:
// (1 --> 2): The sum is 3.
// (1 --> 3): The sum is 4.
// There is no root-to-leaf path with sum = 5.
tree2 := &TreeNode {
1,
&TreeNode{2, nil, nil},
&TreeNode{3, nil, nil},
}
fmt.Println(hasPathSum(tree2,6)) // false
// Example 3:
// Input: root = [], targetSum = 0
// Output: false
// Explanation: Since the tree is empty, there are no root-to-leaf paths.
fmt.Println(hasPathSum(nil,0)) // false
fmt.Println(hasPathSum1(tree1,22)) // true
fmt.Println(hasPathSum1(tree2,6)) // false
fmt.Println(hasPathSum1(nil,0)) // false
}