• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode257.二叉树的所有路径|BinaryTreePaths

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

Input:

   1
 /   \
2     3
 \
  5

Output: ["1->2->5", "1->3"]

Explanation: All root-to-leaf paths are: 1->2->5, 1->3

给定一个二叉树,返回所有从根节点到叶子节点的路径。

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

示例:

输入:

   1
 /   \
2     3
 \
  5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

16ms
 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 binaryTreePaths(_ root: TreeNode?) -> [String] {
16         var list:[String] = [String]()
17         recuesive(root,&list,String())
18         return list
19     }
20     func recuesive(_ root:TreeNode?,_ list:inout [String],_ str:String)
21     {
22         if root == nil {return}
23         var strNew:String = str
24         var strRoot:String = String(root!.val)
25         if root?.left == nil && root?.right == nil
26         {
27             strNew = strNew + strRoot
28             list.append(strNew)
29             return
30         }
31         strRoot = strNew + strRoot + "->"
32         recuesive(root?.left,&list,strRoot)
33         recuesive(root?.right,&list,strRoot)
34     }
35 }

16ms

 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 binaryTreePaths(_ root: TreeNode?) -> [String] {
16         guard let root = root else {
17             return []
18         }
19         var result = [String]()
20         binaryTreePathsDFS(root, "", &result)
21         return result
22     }
23     
24     func binaryTreePathsDFS(_ root: TreeNode, _ out: String, _ result: inout [String]) {
25         if root.left == nil && root.right == nil {
26             result.append(out + String(root.val))
27         }
28         
29         if root.left != nil {
30             binaryTreePathsDFS(root.left!, out + String(root.val) + "->", &result)
31         }
32         if root.right != nil {
33             binaryTreePathsDFS(root.right!, out + String(root.val) + "->", &result)
34         }
35         
36     }
37 }

16ms

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
云存储(Swift+Keystone)部署策略发布时间:2022-07-13
下一篇:
[Swift]LeetCode782.变为棋盘|TransformtoChessboard发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap