Golang concise solution using backtracking recursion


  • 0

    Unless misunderstanding the meaning of "leaf" (have no children), this problem is straight-forward.

    func hasPathSum(root *TreeNode, sum int) bool {
    	res := false
    	doHasPathSum(root, 0, sum, &res)
    	return res
    }
    
    func doHasPathSum(parent *TreeNode, currentSum int, targetSum int, result *bool) {
    	if parent == nil {
    		return
    	}
    	currentSum += parent.Val
    	if *result || (currentSum == targetSum && parent.Left == nil && parent.Right == nil) {
    		*result = true
    		return
    	}
    	doHasPathSum(parent.Left, currentSum, targetSum, result)
    	doHasPathSum(parent.Right, currentSum, targetSum, result)
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.