leetcode-112 路径总和 2015年03月01日 Tree 留言 112. 路径总和 难度: 简单 递归求解即可 12345678910111213141516171819202122/** * 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 targetSum == root.Val } return hasPathSum(root.Left, targetSum-root.Val) || hasPathSum(root.Right, targetSum-root.Val)} 文章目录 原文链接: https://dashen.tech/2015/03/01/leetcode-112-路径总和/ 版权声明: 转载请注明出处.