在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Print a binary tree in an m*n 2D string array following these rules:
Example 1: Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]] Example 2: Input: 1 / \ 2 3 \ 4 Output: [["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""]] Example 3: Input: 1 / \ 2 5 / 3 / 4 Output: [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""] ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""] ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""] ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]] Note: The height of binary tree is in the range of [1, 10]. 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则:
示例 1: 输入: 1 / 2 输出: [["", "1", ""], ["2", "", ""]] 示例 2: 输入: 1 / \ 2 3 \ 4 输出: [["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""]] 示例 3: 输入: 1 / \ 2 5 / 3 / 4 输出: [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""] ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""] ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""] ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]] 注意: 二叉树的高度在范围 [1, 10] 中。 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 printTree(_ root: TreeNode?) -> [[String]] { 16 let h = getHeight(root) 17 let w = 2 << (h - 1) - 1 18 var res = [[String]]() 19 for _ in 0 ..< h { 20 let row = [String].init(repeating: "", count: w) 21 res.append(row) 22 } 23 fill(root, &res, 0, 0, w - 1) 24 return res 25 } 26 27 private func getHeight(_ root: TreeNode?) -> Int { 28 guard let node = root else { 29 return 0 30 } 31 return max(getHeight(node.left), getHeight(node.right)) + 1 32 } 33 34 private func fill(_ root: TreeNode?, _ grid: inout [[String]], _ h: Int, _ l: Int, _ r: Int) { 35 guard let node = root else { 36 return 37 } 38 let mid = (l + r) / 2 39 grid[h][mid] = String(node.val) 40 fill(node.left, &grid, h + 1, l, mid - 1) 41 fill(node.right, &grid, h + 1, mid + 1, r) 42 } 43 } Runtime: 20 ms
Memory Usage: 19.2 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 printTree(_ root: TreeNode?) -> [[String]] { 16 var h:Int = getHeight(root) 17 var w:Int = Int(pow(2,Double(h)) - 1) 18 var res:[[String]] = [[String]](repeating:[String](repeating:String(),count:w),count:h) 19 helper(root, 0, w - 1, 0, h, &res) 20 return res 21 } 22 23 func getHeight(_ node: TreeNode?) -> Int 24 { 25 if node == nil {return 0} 26 return 1 + max(getHeight(node?.left), getHeight(node?.right)) 27 } 28 29 func helper(_ node: TreeNode?,_ i:Int,_ j:Int,_ curH:Int,_ height:Int,_ res:inout [[String]]) 30 { 31 if node == nil || curH == height {return} 32 res[curH][(i + j) / 2] = String(node!.val) 33 helper(node!.left, i, (i + j) / 2, curH + 1, height, &res) 34 helper(node!.right, (i + j) / 2 + 1, j, curH + 1, height, &res) 35 } 36 } 20ms 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 printTree(_ root: TreeNode?) -> [[String]] { 16 let depth = depthTree(root) 17 return helper(depth, root) 18 } 19 20 private func helper(_ depth: Int, _ root: TreeNode?) -> [[String]] { 21 if depth <= 0 { return [] } 22 let width = Int(pow(2, Double(depth))) - 1 23 var str = [String](repeating: "", count: width) 24 if let root = root { 25 let middle = width/2 26 str[middle] = "\(root.val)" 27 } 28 if depth == 1 { return [str] } 29 var res = [[String]]() 30 res.append(str) 31 let resLeft = helper(depth - 1, root?.left) 32 let resRight = helper(depth - 1, root?.right) 33 for i in 0..<resLeft.count { 34 res.append(resLeft[i] + [""] + resRight[i]) 35 } 36 return res 37 } 38 39 private func depthTree(_ root: TreeNode?) -> Int { 40 guard let root = root else { return 0 } 41 return 1 + max(depthTree(root.left), depthTree(root.right)) 42 } 43 }
|
请发表评论