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)
}
相关推荐
农场主John2 小时前
(栈)Leetcode155最小栈+739每日温度
windows·python·算法·leetcode·
杜子不疼.5 小时前
【LeetCode 415】—字符串相加算法详解
算法·leetcode·职场和发展
仙俊红5 小时前
LeetCode每日一题,2025-08-21
算法·leetcode·职场和发展
一起努力啊~6 小时前
算法题打卡力扣第34题:在排序数组中查找元素的第一个和最后一个位置(mid)
数据结构·算法·leetcode
1白天的黑夜17 小时前
链表-143.重排链表-力扣(LeetCode)
数据结构·leetcode·链表
快去睡觉~15 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧15 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油15 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
元亓亓亓20 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
1白天的黑夜11 天前
链表-24.两两交换链表中的结点-力扣(LeetCode)
数据结构·leetcode·链表