/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcpathSum(root*TreeNode,targetint)[][]int{varans[][]intifnil==root{returnans}parent:=make(map[*TreeNode]*TreeNode)buildPath:=func(node*TreeNode)[]int{varans[]intfornode!=nil{ans=append(ans,node.Val)node=parent[node]}fori:=0;i<len(ans)/2;i++{ans[i],ans[len(ans)-1-i]=ans[len(ans)-1-i],ans[i]}returnans}parent[root]=nilvardfsfunc(root*TreeNode,curint)dfs=func(root*TreeNode,curint){ifroot.Left==nil&&root.Right==nil{ifcur+root.Val==target{ans=append(ans,buildPath(root))}return}ifroot.Left!=nil{parent[root.Left]=rootdfs(root.Left,root.Val+cur)}ifroot.Right!=nil{parent[root.Right]=rootdfs(root.Right,root.Val+cur)}}dfs(root,0)returnans}