★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9951816.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum 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  5   1

Return:

[[5,4,11,2],[5,8,4,5]
]

给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

示例:
给定如下二叉树,以及目标和 sum = 22

              5/ \4   8/   / \11  13  4/  \    / \7    2  5   1

返回:

[[5,4,11,2],[5,8,4,5]
]

24ms
 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 pathSum(_ root: TreeNode?, _ sum: Int) -> [[Int]] {
16         var resArray =  [[Int]]()
17         if root == nil {
18             return resArray
19         }
20
21         if root?.left == nil && root?.right == nil {
22             if root!.val == sum {
23                 let nodeArray = [Int](repeating:root!.val,count:1)
24                 resArray.append(nodeArray)
25             }
26             return resArray
27         }
28
29         var nodeArray =  [Int]()
30         helper(root,sum,&resArray,&nodeArray)
31         return resArray
32     }
33
34     func helper(_ root: TreeNode?, _ sum: Int, _ array: inout [[Int]], _ nodeArray: inout [Int]) {
35         if root == nil {
36             return
37         }
38
39         if root?.left == nil && root?.right == nil {
40             if sum-root!.val == 0 {
41                 nodeArray.append(root!.val)
42                 array.append(nodeArray)
43                 nodeArray.remove(at:nodeArray.count-1)
44             }
45             return
46         }
47
48         nodeArray.append(root!.val)
49         helper(root?.left,sum-root!.val,&array,&nodeArray)
50         helper(root?.right,sum-root!.val,&array,&nodeArray)
51         let index = nodeArray.count - 1
52         nodeArray.remove(at:index)
53     }
54 }


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 pathSum(_ root: TreeNode?, _ sum: Int) -> [[Int]] {
16         var ans = [[Int]]()
17         var combination = [Int]()
18         pathSum(root, sum, &combination, &ans)
19         return ans
20     }
21
22     func pathSum(_ node: TreeNode?, _ sum: Int, _ combination: inout [Int], _ ans: inout [[Int]]) {
23         guard let node = node else { return }
24
25         combination.append(node.val)
26
27         if node.left == nil && node.right == nil && (sum - node.val) == 0 {
28             ans.append(combination)
29         }
30
31         pathSum(node.left, sum - node.val, &combination, &ans)
32         pathSum(node.right, sum - node.val, &combination, &ans)
33
34         combination.removeLast()
35     }
36 }


32ms

 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 pathSum(_ root: TreeNode?, _ sum: Int) -> [[Int]] {
16         if root == nil { return []}
17         var result = [[Int]]()
18         var currentRun = [Int]()
19         checkSum(root, sum, &result, &currentRun)
20         return result
21
22     }
23     func checkSum(_ root: TreeNode?, _ sum: Int,_ result: inout [[Int]],_ currentRun: inout [Int] ) {
24         if root == nil { return }
25         var current = currentRun
26         current.append(root!.val)
27         if sum - root!.val == 0 && root!.left == nil && root!.right == nil {
28             result.append(current)
29             return
30         }
31         checkSum(root!.left, sum - root!.val, &result, &current)
32         checkSum(root!.right, sum - root!.val, &result, &current)
33     }
34 }


52ms

 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 pathSum(_ root: TreeNode?, _ sum: Int) -> [[Int]] {
16         var paths: [[Int]] = []
17         BFS(root, sum: sum, paths: &paths)
18         return paths
19     }
20
21     func BFS(_ root: TreeNode?, sum: Int, paths: inout [[Int]], path: [Int]? = nil) {
22         guard let root = root else { return }
23         var path: [Int] = path ?? []
24         path.append(root.val)
25         if root.left == nil && root.right == nil {
26             if sum == path.reduce(0) { $0 + $1 } {
27                 paths.append(path)
28             }
29             return
30         }
31         BFS(root.left, sum: sum, paths: &paths, path: path)
32         BFS(root.right, sum: sum, paths: &paths, path: path)
33     }
34 }


56ms

 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 pathSum(_ root: TreeNode?, _ sum: Int) -> [[Int]] {
16         guard let root = root else {
17             return []
18         }
19
20         if root.left == nil && root.right == nil && root.val == sum {
21             return [[sum]]
22         }
23
24         let lPathSum = pathSum(root.left, sum - root.val)
25         let rPathSum = pathSum(root.right, sum - root.val)
26
27         return (lPathSum + rPathSum).map {
28             [root.val] + $0
29         }
30     }
31 }

转载于:https://www.cnblogs.com/strengthen/p/9951816.html

[Swift]LeetCode113. 路径总和 II | Path Sum II相关推荐

  1. leetcode 113. 路径总和 II(Path Sum II)

    目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及 ...

  2. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  3. [LeetCode]113.Path Sum II

    [题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...

  4. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  5. 113. Path Sum II

    /** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...

  6. LeetCode 113. Path Sum II

    113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...

  7. leetcode113. 路径总和 II

    113. 路径总和 II 难度中等163 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标 ...

  8. leetcode113. 路径总和 II(dfs)

    给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径.说明: 叶子节点是指没有子节点的节点.示例: 给定如下二叉树,以及目标和 sum = 22,5/ \4 8/ / \ ...

  9. LeetCode113. 路径总和 II(DFS)(递归)

    题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 思路 详见链接 代码 class TreeNode:def __ ...

最新文章

  1. CCF-CSP 201903-1 小中大(C++满分代码)
  2. 30 天精通 RxJS (01):认识 RxJS
  3. java强制执行方法_java – 在多台机器上强制执行单一速率限制的好方法是什么?...
  4. for循环批量写文件 shell_shell脚本:for循环批量重命名带空格文件名的文件
  5. BZOJ 3203 Sdoi2013 保护出题人 凸包+三分
  6. 中国磷酸一铵(MAP)行业市场供需与战略研究报告
  7. Knockout自定义绑定my97datepicker
  8. 个人微信机器人,微信开发API
  9. DELL VENUE 11 7130解锁功耗墙总结
  10. PVID和VID的理解
  11. react中使用构建缓存_如何使用React,GraphQL和Okta构建健康跟踪应用
  12. 曾仕强《领导的沟通艺术》读书笔记
  13. background-clip属性详解
  14. Android开发之ViewFlipper
  15. 购物篮数据两种商品间的关联分析
  16. 关于生命和人工智能的一些遐想
  17. 1024程序员节,过节也要写代码呀!
  18. 【读书笔记】《pattern hatching》对设计模式的十大误解
  19. (POJ - 3579)Median(二分)
  20. 电脑一打开wps就黑屏_如何解决电脑打开黑屏只显示鼠标的问题

热门文章

  1. convert.todatetime指定日期格式_java组件huTool日期DateUtil工具的使用
  2. mysql group by_MySQL优化GROUP BY方案
  3. excel数据库_EXCEL憋出大招,逆袭大数据的黑马出现了
  4. docker 保存 环境持久化_为什么 Docker 适合微服务架构?
  5. mybatis一个怪异的问题: Invalid bound statement (not found)
  6. 充值加油卡骗局:一次伪金融诈骗为何能圈数亿
  7. Spotlight on unix 安装
  8. 小记:《技术进步引发的灵感革命》网易游戏学院第二届公开日
  9. 【转】glTexImage2D()和gluBuild2DMipmaps() [将载入的位图文件(*.bmp)转换成纹理贴图]+glTexParameteri()纹理过滤函数...
  10. Asterisk目录结构如下