mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
20 lines
397 B
Go
20 lines
397 B
Go
package leetcode
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
* type TreeNode struct {
|
|
* Val int
|
|
* Left *TreeNode
|
|
* Right *TreeNode
|
|
* }
|
|
*/
|
|
func hasPathSum(root *TreeNode, sum int) bool {
|
|
if root == nil {
|
|
return false
|
|
}
|
|
if root.Left == nil && root.Right == nil {
|
|
return sum == root.Val
|
|
}
|
|
return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val)
|
|
}
|