-
Notifications
You must be signed in to change notification settings - Fork 77
/
113.路径总和-ii.go
79 lines (76 loc) · 1.51 KB
/
113.路径总和-ii.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
/*
* @lc app=leetcode.cn id=113 lang=golang
*
* [113] 路径总和 II
*
* https://leetcode-cn.com/problems/path-sum-ii/description/
*
* algorithms
* Medium (55.11%)
* Likes: 88
* Dislikes: 0
* Total Accepted: 9.5K
* Total Submissions: 17.2K
* Testcase Example: '[5,4,8,11,null,13,4,7,2,null,null,5,1]\n22'
*
* 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
*
* 说明: 叶子节点是指没有子节点的节点。
*
* 示例:
* 给定如下二叉树,以及目标和 sum = 22,
*
* 5
* / \
* 4 8
* / / \
* 11 13 4
* / \ / \
* 7 2 5 1
*
*
* 返回:
*
* [
* [5,4,11,2],
* [5,8,4,5]
* ]
*
*
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func pathSum(root *TreeNode, sum int) [][]int {
s := &solution{
ret: [][]int{},
path: []int{},
}
s.helper(root, sum)
return s.ret
}
type solution struct {
ret [][]int
path []int
}
func (s *solution) helper(root *TreeNode, sum int) {
if root == nil {
return
}
rest := sum - root.Val
s.path = append(s.path, root.Val)
defer func() {
s.path = s.path[0 : len(s.path)-1]
}()
if root.Left == nil && root.Right == nil && rest == 0 {
s.ret = append(s.ret, append([]int{}, s.path...))
return
}
s.helper(root.Left, rest)
s.helper(root.Right, rest)
}