[Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10455628.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.
The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.
Example 1:
Input: Binary tree: [1,2,3,4] 1 / \ 2 3 / 4 Output: "1(2(4))(3)"
Explanation: Originallay it needs to be "1(2(4)())(3()())",
but you need to omit all the unnecessary empty parenthesis pairs.
And it will be "1(2(4))(3)".
Example 2:
Input: Binary tree: [1,2,3,null,4] 1 / \ 2 3 \ 4 Output: "1(2()(4))(3)"
Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。
空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
示例 1:
输入: 二叉树: [1,2,3,4] 1 / \ 2 3 / 4 输出: "1(2(4))(3)" 解释: 原本将是“1(2(4)())(3())”, 在你省略所有不必要的空括号对之后, 它将是“1(2(4))(3)”。
示例 2:
输入: 二叉树: [1,2,3,null,4] 1 / \ 2 3 \ 4 输出: "1(2()(4))(3)" 解释: 和第一个示例相似, 除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。
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 tree2str(_ t: TreeNode?) -> String { 16 if t == nil {return String()} 17 var res:String = String(t!.val) 18 if t!.left == nil && t!.right == nil {return res} 19 res += "(" + tree2str(t!.left) + ")" 20 if t!.right != nil 21 { 22 res += "(" + tree2str(t!.right) + ")" 23 } 24 return res 25 } 26 }
84ms
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 tree2str(_ t: TreeNode?) -> String { 16 guard t != nil else { 17 return "" 18 } 19 var res = "" 20 inorderMakeString(t!, &res) 21 return res 22 } 23 24 private func inorderMakeString(_ t: TreeNode?, _ str: inout String) { 25 26 guard let node = t else { 27 return 28 } 29 str += String(node.val) 30 if node.left == nil && node.right == nil { 31 return 32 } 33 34 if node.right != nil || node.left != nil { 35 str += "(" 36 inorderMakeString(node.left, &str) 37 str += ")" 38 39 } 40 if node.right != nil { 41 str += "(" 42 inorderMakeString(node.right!, &str) 43 str += ")" 44 } 45 } 46 }
88ms
1 class Solution { 2 func tree2str(_ t: TreeNode?) -> String { 3 guard let t = t else { 4 return "" 5 } 6 if isLeaf(t) { 7 let s = "\(t.val)" 8 // print(s) 9 return s 10 } else if let left = t.left, let right = t.right { 11 let s = "\(t.val)(\(tree2str(left)))(\(tree2str(right)))" 12 // print(s) 13 return s 14 } else if let left = t.left { 15 let s = "\(t.val)(\(tree2str(left)))" 16 // print(s) 17 return s 18 } else if let right = t.right { 19 let s = "\(t.val)()(\(tree2str(right)))" 20 // print(s) 21 return s 22 } 23 else { 24 return "" 25 } 26 } 27 28 func isLeaf(_ t: TreeNode) -> Bool { 29 return t.left == nil && t.right == nil 30 } 31 }
92ms
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 tree2str(_ t: TreeNode?) -> String { 16 guard let node = t else { 17 return "" 18 } 19 var result = "\(node.val)" 20 if let left = node.left { 21 result+="(\(tree2str(left)))" 22 } 23 if let right = node.right { 24 if node.left == nil { 25 result += "()" 26 } 27 result+="(\(tree2str(right)))" 28 } 29 return result 30 } 31 }
96ms
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 tree2str(_ t: TreeNode?) -> String { 16 guard let t = t else { return "" } 17 if t.right == nil && t.left == nil { return "\(t.val)" } 18 var toReturn = "\(t.val)(\(tree2str(t.left)))" 19 if t.right != nil { 20 toReturn += "(\(tree2str(t.right)))" 21 } 22 return toReturn 23 } 24 }
128ms
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 tree2str(_ t: TreeNode?) -> String { 16 var result: String = "" 17 preorder(node: t, result: &result) 18 return result 19 } 20 21 private func preorder(node: TreeNode?, result: inout String) { 22 if node == nil { return } 23 result = result.appending(String(node!.val)) 24 25 if node?.left != nil || node?.right != nil { 26 result = result.appending("(") 27 preorder(node: node?.left, result: &result) 28 result = result.appending(")") 29 } 30 31 if node?.right != nil { 32 result = result.appending("(") 33 preorder(node: node?.right, result: &result) 34 result = result.appending(")") 35 } 36 } 37 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了