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

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)"
解释: 和第一个示例相似,
除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。

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 = 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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10455628.html