-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0297-serialize-and-deserialize-binary-tree.swift
48 lines (42 loc) · 1.43 KB
/
0297-serialize-and-deserialize-binary-tree.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/
class Codec {
func preorderTraversal(_ root: TreeNode?) -> String {
guard let root = root else { return "x" }
return "\(root.val)" + " " + preorderTraversal(root.left) + " " + preorderTraversal(root.right)
}
func serialize(_ root: TreeNode?) -> String {
return preorderTraversal(root)
}
func preorderTraversal(_ data: [String], _ index: inout Int) -> TreeNode? {
guard index < data.count else { return nil }
guard data[index] != "x" else { return nil }
guard let val = Int(data[index]) else { return nil }
let current = TreeNode(val)
index += 1
current.left = preorderTraversal(data, &index)
index += 1
current.right = preorderTraversal(data, &index)
return current
}
func deserialize(_ data: String) -> TreeNode? {
guard !data.isEmpty else { return nil }
var index = 0
return preorderTraversal(data.split(separator: " ").map { String($0) }, &index)
}
}
// Your Codec object will be instantiated and called as such:
// var ser = Codec()
// var deser = Codec()
// deser.deserialize(ser.serialize(root))