/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funchasPathSum(root*TreeNode,targetSumint)bool{ifroot==nil{returnfalse}vardfsfunc(root*TreeNode,targetSumint)booldfs=func(root*TreeNode,targetSumint)bool{ifroot==nil{iftargetSum==0{returntrue}returnfalse}targetSum-=root.Valleft:=dfs(root.Left,targetSum)ifleft{returntrue}right:=dfs(root.Right,targetSum)returnright}returndfs(root,targetSum)}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funchasPathSum(root*TreeNode,targetSumint)bool{ifroot!=nil{targetSum-=root.Valifroot.Left==nil&&root.Right==nil{iftargetSum==0{returntrue}returnfalse}left:=hasPathSum(root.Left,targetSum)ifleft{returntrue}right:=hasPathSum(root.Right,targetSum)returnright}returnfalse}