★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mxfjqmyd-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.node
Note: A leaf is a node with no children.git
Example:github
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.spa
給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上全部節點值相加等於目標和。code
說明: 葉子節點是指沒有子節點的節點。htm
示例:
給定以下二叉樹,以及目標和 sum = 22
,blog
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
返回 true
, 由於存在目標和爲 22 的根節點到葉子節點的路徑 5->4->11->2
。get
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 }