mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
28 lines
512 B
Go
28 lines
512 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* 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)
|
|
}
|