[Swift]LeetCode112. 路径总和 | Path Sum
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9709165.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Note: A leaf is a node with no children.
Example:
Given the below binary tree and sum = 22
,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和 sum = 22
,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
返回 true
, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2
。
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func hasPathSum(_ root: TreeNode?, _ sum: Int) -> Bool { 16 if root == nil {return false} 17 return check(root,0,sum) 18 } 19 func check(_ node:TreeNode?,_ sum:Int,_ cur:Int)->Bool 20 { 21 if node == nil && sum == cur 22 { 23 return true 24 } 25 if node == nil 26 { 27 return false 28 } 29 if node!.left == nil && node!.right != nil 30 { 31 return check(node!.right,sum+node!.val,cur) 32 } 33 if node!.left != nil && node!.right == nil 34 { 35 return check(node!.left,sum+node!.val,cur) 36 } 37 return check(node!.left,sum + node!.val, cur) 38 || check(node!.right,sum + node!.val, cur) 39 } 40 }
36ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func hasPathSum(_ root: TreeNode?, _ sum: Int) -> Bool { 16 if root?.left == nil, root?.right == nil { // 叶节点或空节点 17 guard root != nil else { 18 return false 19 } 20 return sum == root!.val ? true : false 21 } 22 return hasPathSum(root?.left, sum - root!.val) || hasPathSum(root?.right, sum - root!.val) 23 } 24 }
28ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func hasPathSum(_ root: TreeNode?, _ sum: Int) -> Bool { 16 17 guard let root = root else { return false } 18 19 let difference = sum - root.val 20 if difference == 0 && root.left == nil && root.right == nil { 21 return true 22 } else { 23 return hasPathSum(root.left, difference) || hasPathSum(root.right, difference) 24 } 25 26 return false 27 } 28 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了