-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path113.路径总和-ii.go
89 lines (88 loc) · 1.98 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
80
81
82
83
84
85
86
87
88
/*
* @lc app=leetcode.cn id=113 lang=golang
*
* [113] 路径总和 II
*
* https://leetcode-cn.com/problems/path-sum-ii/description/
*
* algorithms
* Medium (55.04%)
* Likes: 84
* Dislikes: 0
* Total Accepted: 8.9K
* Total Submissions: 16.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 {
if root == nil {
return [][]int{}
}
if root.Left == nil && root.Right == nil {
if root.Val == sum {
return [][]int{
[]int{sum},
}
} else {
return [][]int{}
}
}
if root.Left == nil && root.Right != nil {
rightPaths := pathSum(root.Right, sum-root.Val)
result := make([][]int, 0)
for _, path := range rightPaths {
result = append(result, append([]int{root.Val}, path...))
}
return result
}
if root.Left != nil && root.Right == nil {
leftPaths := pathSum(root.Left, sum-root.Val)
result := make([][]int, 0)
for _, path := range leftPaths {
result = append(result, append([]int{root.Val}, path...))
}
return result
}
result := make([][]int, 0)
leftPaths := pathSum(root.Left, sum-root.Val)
for _, path := range leftPaths {
result = append(result, append([]int{root.Val}, path...))
}
rightPaths := pathSum(root.Right, sum-root.Val)
for _, path := range rightPaths {
result = append(result, append([]int{root.Val}, path...))
}
return result
}