Path Sum

ID: 112; easy

Solution 1

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func hasPathSum(root *TreeNode, targetSum int) bool {
    if root == nil {
        return false
    }
    if root.Left == nil && root.Right == nil {
        return root.Val == targetSum
    }
    curVal := targetSum - root.Val
    return hasPathSum(root.Left, curVal) || hasPathSum(root.Right, curVal)
}

Last updated