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)
}
相关推荐
YoungHong19927 小时前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
im_AMBER8 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
LYFlied9 小时前
【每日算法】LeetCode 25. K 个一组翻转链表
算法·leetcode·链表
LYFlied12 小时前
【每日算法】LeetCode 19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
zfj32113 小时前
vscode是js开发的,为什么能支持golang java等各种语言开发
javascript·vscode·golang
努力学算法的蒟蒻13 小时前
day35(12.16)——leetcode面试经典150
算法·leetcode·面试
serendipity_hky14 小时前
【go语言 | 第5篇】channel——多个goroutine之间通信
开发语言·后端·golang
LYFlied14 小时前
【每日算法】LeetCode 234. 回文链表详解
算法·leetcode·链表
源代码•宸14 小时前
分布式缓存-GO(简历写法、常见面试题)
服务器·开发语言·经验分享·分布式·后端·缓存·golang