Swift - beat 100%
\nclass Codec {\n    // Encodes a tree to a single string.\n    func serialize(_ root: TreeNode?) -> String {\n        var arr = [String]()\n        serialize(r
```\nclass Codec {\n    // Encodes a tree to a single string.\n    func serialize(_ root: TreeNode?) -> String {\n        var arr = [String]()\n        serialize(root, &arr)\n        return arr.joined(separator: ",")\n    }\n    \n    func serialize(_ root: TreeNode?, _ arr: inout [String]) {        \n        guard let root = root else{\n            return\n        }\n        arr.append("\\(root.val)")\n        serialize(root.left, &arr)\n        serialize(root.right, &arr)\n    }\n    \n    // Decodes your encoded data to tree.\n    func deserialize(_ data: String) -> TreeNode? {\n         guard data != "" else{\n            return nil\n        }\n        \n        let arr = data.components(separatedBy:",")\n        var index = 0\n        return deserialize(arr, &index, Int.min, Int.max)\n    }\n    \n    func deserialize(_ arr: [String], _ index: inout Int, _ minVal: Int, _ maxVal: Int) -> TreeNode? {\n        guard index < arr.count, let val = Int(arr[index]) else{\n            return nil\n        }\n        \n        if val < minVal || val > maxVal{\n            return nil\n        }\n        \n        let node = TreeNode(val)\n        index += 1\n        \n        node.left = deserialize(arr, &index, minVal, val)\n        node.right = deserialize(arr, &index, val, maxVal)\n        \n        return node\n    }\n}\n```\n
[]