Golang | Leetcode Golang题解之第112题路径总和

题目:

题解:

Go 复制代码
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)
}
相关推荐
Kuo-Teng13 小时前
LeetCode 198: House Robber
java·算法·leetcode·职场和发展·动态规划
橘颂TA14 小时前
【剑斩OFFER】算法的暴力美学——除自身以外数组的乘积
算法·leetcode·职场和发展·结构与算法
Espresso Macchiato16 小时前
Leetcode 3748. Count Stable Subarrays
算法·leetcode·职场和发展·leetcode hard·leetcode 3748·leetcode周赛476·区间求和
脉动数据行情19 小时前
Go语言对接股票、黄金、外汇API实时数据教程
开发语言·后端·golang
天选之女wow19 小时前
【Hard——Day4】25.K 个一组翻转链表
数据结构·算法·leetcode·链表
Dream it possible!1 天前
LeetCode 面试经典 150_二叉树_二叉树中的最大路径和(77_124_C++_困难)(DFS)
c++·leetcode·面试·二叉树
做怪小疯子1 天前
LeetCode 热题 100——子串——和为 K 的子数组
算法·leetcode·职场和发展
希望有朝一日能如愿以偿1 天前
力扣每日一题:仅含1的子串数
算法·leetcode·职场和发展
q***71081 天前
【Golang】——Gin 框架中的表单处理与数据绑定
microsoft·golang·gin