题解 | #二叉树中是否存在节点和为指定值的路径#
二叉树中是否存在节点和为指定值的路径
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)
}
阿里云工作强度 727人发布