找BUG 深度优先搜索 切片BUG 可变参数
https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func pathSum(root *TreeNode, target int) [][]int { if root == nil { return nil } var l [][]int var dfs func(t *TreeNode, v int, ll ...int) dfs = func(t *TreeNode, v int, ll ...int) { if t == nil { return } v += t.Val if v > target { return } if v == target { ll = append(ll, t.Val) l = append(l, ll) return } ll = append(ll, t.Val) dfs(t.Left, v, ll...) dfs(t.Right, v, ll...) return } dfs(root, 0) return l }
1、题意理解
2、取值范围
3、语法
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func pathSum(root *TreeNode, target int) [][]int { if root == nil { return nil } var l [][]int var dfs func(t *TreeNode, v int, ll ...int) dfs = func(t *TreeNode, v int, ll ...int) { if t == nil { return } v += t.Val f := func() []int { l := append([]int{}, ll...) l = append(l, t.Val) return l } if v == target { if t.Left == nil && t.Right == nil { l = append(l, f()) return } } dfs(t.Left, v, f()...) dfs(t.Right, v, f()...) return } dfs(root, 0) return l }