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-Teng1 小时前
LeetCode 141. Linked List Cycle
java·算法·leetcode·链表·职场和发展
资深web全栈开发1 小时前
力扣2536子矩阵元素加1-差分数组解法详解
算法·leetcode·矩阵·golang·差分数组
·云扬·2 小时前
【LeetCode Hot 100】 136. 只出现一次的数字
算法·leetcode·职场和发展
熬了夜的程序员4 小时前
【LeetCode】114. 二叉树展开为链表
leetcode·链表·深度优先
大胆飞猪8 小时前
递归、剪枝、回溯算法---全排列、子集问题(力扣.46,78)
算法·leetcode·剪枝
Swift社区11 小时前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展
Kuo-Teng14 小时前
LeetCode 206: Reverse Linked List
java·算法·leetcode·职场和发展
做怪小疯子18 小时前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
Dream it possible!18 小时前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
做怪小疯子18 小时前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展