-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpath_sum_II.go
67 lines (53 loc) · 1.64 KB
/
path_sum_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
package main
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func pathSum(root *TreeNode, targetSum int) [][]int {
var allPaths [][]int
var currentPath []int
helper(root, currentPath, &allPaths, targetSum)
return allPaths
}
func helper(root *TreeNode, currentPath []int, allPaths *[][]int, targetSum int) {
if root == nil {
return
}
newCurrentPath := make([]int, len(currentPath))
copy(newCurrentPath, currentPath)
newCurrentPath = append(newCurrentPath, root.Val)
if root.Val == targetSum && root.Left == nil && root.Right == nil {
*allPaths = append(*allPaths, newCurrentPath)
} else {
helper(root.Left, newCurrentPath, allPaths, targetSum-root.Val)
helper(root.Right, newCurrentPath, allPaths, targetSum-root.Val)
}
}
// func pathSum(root *TreeNode, targetSum int) [][]int {
// solution := make([][]int, 0)
// if root == nil {
// return solution
// }
// return pathSumWith(root, targetSum, []int{}, solution)
// }
// func pathSumWith(node *TreeNode, targetSum int, path []int, solution [][]int) [][]int {
// if node == nil {
// return solution
// }
// targetSum -= node.Val
// newPath := make([]int, len(path))
// copy(newPath, path)
// newPath = append(newPath, node.Val)
// if node.Left == nil && node.Right == nil {
// if targetSum == 0 {
// solution = append(solution, newPath)
// }
// return solution
// }
// return append(
// pathSumWith(node.Left, targetSum, newPath, solution),
// pathSumWith(node.Right, targetSum, newPath, solution)...,
// )
// }