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)
}
相关推荐
CoderYanger2 小时前
动态规划算法-01背包问题:50.分割等和子集
java·算法·leetcode·动态规划·1024程序员节
菜鸟233号4 小时前
力扣513 找树左下角的值 java实现
java·数据结构·算法·leetcode
leoufung4 小时前
LeetCode 22:Generate Parentheses 题解(DFS / 回溯)
算法·leetcode·深度优先
FMRbpm4 小时前
队列练习--------最近的请求次数(LeetCode 933)
数据结构·c++·leetcode·新手入门
长安er6 小时前
LeetCode 34排序数组中查找元素的第一个和最后一个位置-二分查找
数据结构·算法·leetcode·二分查找·力扣
CoderYanger6 小时前
动态规划算法-两个数组的dp(含字符串数组):48.最长重复子数组
java·算法·leetcode·动态规划·1024程序员节
sin_hielo7 小时前
leetcode 3577
数据结构·算法·leetcode
慕容青峰8 小时前
【LeetCode 1925. 统计平方和三元组的数目 题解】
c++·算法·leetcode
独自破碎E9 小时前
如何用最短替换让字符串变平衡?
java·开发语言·算法·leetcode
Swift社区9 小时前
LeetCode 446 - 等差数列划分 II - 子序列
算法·leetcode·职场和发展