题解 | #二叉树中是否存在节点和为指定值的路径#

二叉树中是否存在节点和为指定值的路径

http://www.nowcoder.com/practice/508378c0823c423baa723ce448cbfd0c

package main
import . "nc_tools"


func hasPathSum( root *TreeNode ,  sum int ) bool {
    // write code here
    if root == nil {
        return false
    }
    if root.Left == nil && root.Right == nil {
        return root.Val == sum
    }

    return hasPathSum(root.Left, sum - root.Val) || hasPathSum(root.Right, sum - root.Val)
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务