★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-hucmfqct-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.node
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.git
Example 1:github
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.
你須要採用前序遍歷的方式,將一個二叉樹轉換成一個由括號和整數組成的字符串。微信
空節點則用一對空括號 "()" 表示。並且你須要省略全部不影響字符串與原始二叉樹之間的一對一映射關係的空括號對。app
示例 1:spa
輸入: 二叉樹: [1,2,3,4] 1 / \ 2 3 / 4 輸出: "1(2(4))(3)" 解釋: 本來將是「1(2(4)())(3())」, 在你省略全部沒必要要的空括號對以後, 它將是「1(2(4))(3)」。
示例 2:code
輸入: 二叉樹: [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 }
84mshtm
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 }