[Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree

[Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree,第1张

概述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 n

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,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,4]       1     /       2     3   /      4     输出: "1(2(4))(3)"解释: 原本将是“1(2(4)())(3())”,在你省略所有不必要的空括号对之后,它将是“1(2(4))(3)”。

示例 2:

输入: 二叉树: [1,4]       1     /       2     3     \        4 输出: "1(2()(4))(3)"解释: 和第一个示例相似,除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。
Runtime: 80 ms Memory Usage: 20.7 MB
 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 = nil10  *         self.right = nil11  *     }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 != nil21         {22             res += "(" + tree2str(t!.right) + ")"23         }24         return res25     }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 = nil10  *         self.right = nil11  *     }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 res22     }23     24     private func inorderMakeString(_ t: TreeNode?,_ str: inout String) {25         26         guard let node = t else {27             return28         }29         str += String(node.val)30         if node.left == nil && node.right == nil {31             return32         }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 s10         } else if let left = t.left,let right = t.right {11             let s = "\(t.val)(\(tree2str(left)))(\(tree2str(right)))"12             // print(s)13             return s14         } else if let left = t.left {15             let s =  "\(t.val)(\(tree2str(left)))"16             // print(s)17             return s18         } else if let right = t.right {19             let s =  "\(t.val)()(\(tree2str(right)))"20             // print(s)21             return s22         }23         else {24             return ""25         }26     }27 28     func isLeaf(_ t: TreeNode) -> Bool {29         return t.left == nil && t.right == nil30     }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 = nil10  *         self.right = nil11  *     }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 result30     }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 = nil10  *         self.right = nil11  *     }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 toReturn23     }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 = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     func tree2str(_ t: TreeNode?) -> String {16         var result: String = ""17         preorder(node: t,result: &result)18         return result19     }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 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree全部内容,希望文章能够帮你解决[Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1019903.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存