Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3cf16f9

Browse files
Add solutions
1 parent bfc8801 commit 3cf16f9

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import UIKit
2+
3+
/*
4+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
5+
6+
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
7+
8+
Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
9+
10+
Example 1:
11+
12+
13+
Input: root = [1,2,3,null,null,4,5]
14+
Output: [1,2,3,null,null,4,5]
15+
Example 2:
16+
17+
Input: root = []
18+
Output: []
19+
Example 3:
20+
21+
Input: root = [1]
22+
Output: [1]
23+
Example 4:
24+
25+
Input: root = [1,2]
26+
Output: [1,2]
27+
28+
29+
Constraints:
30+
31+
The number of nodes in the tree is in the range [0, 104].
32+
-1000 <= Node.val <= 1000
33+
34+
*/
35+
36+
37+
public class TreeNode {
38+
public var val: Int
39+
public var left: TreeNode?
40+
public var right: TreeNode?
41+
public init(_ val: Int) {
42+
self.val = val
43+
self.left = nil
44+
self.right = nil
45+
}
46+
}
47+
48+
class Solution {
49+
func serialize(_ root: TreeNode?) -> String {
50+
guard let root = root else { return "#,"}
51+
52+
let left = serialize(root.left)
53+
let right = serialize(root.right)
54+
55+
return "\(root.val),\(left)\(right)"
56+
}
57+
58+
func deserialize(_ data: String) -> TreeNode? {
59+
var values = data.split(separator: ",").map { String(0ドル) }
60+
print(values)
61+
return helper(&values)
62+
}
63+
64+
func helper(_ values: inout [String]) -> TreeNode? {
65+
if values.isEmpty == true { return nil }
66+
guard let value = Int(values.removeFirst()) else {
67+
return nil
68+
}
69+
70+
let node = TreeNode(value)
71+
node.left = helper(&values)
72+
node.right = helper(&values)
73+
74+
return node
75+
}
76+
}
77+
78+
// Your Codec object will be instantiated and called as such:
79+
// var ser = Codec()
80+
// var deser = Codec()
81+
// deser.deserialize(ser.serialize(root))
82+
83+
import Foundation
84+
import XCTest
85+
class SolutionTests: XCTestCase {
86+
var ser: Solution!
87+
var deser: Solution!
88+
override func setUp() {
89+
super.setUp()
90+
ser = Solution()
91+
deser = Solution()
92+
}
93+
94+
func test1() {
95+
// [1,2,3,null,null,4,5]
96+
let node1 = TreeNode(1)
97+
let node2 = TreeNode(2)
98+
let node3 = TreeNode(3)
99+
let node4 = TreeNode(4)
100+
let node5 = TreeNode(5)
101+
node1.left = node2
102+
node1.right = node3
103+
node3.left = node4
104+
node3.right = node5
105+
let results = ser.serialize(node1)
106+
XCTAssertEqual(results, "[]")
107+
108+
deser.deserialize(results)
109+
}
110+
}
111+
112+
SolutionTests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

‎297. Serialize and Deserialize Binary Tree.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation = "file:///Users/zenglekidd/Desktop/297.%20Serialize%20and%20Deserialize%20Binary%20Tree.playground#CharacterRangeLen=1155&amp;CharacterRangeLoc=1127&amp;EndingColumnNumber=41&amp;EndingLineNumber=80&amp;StartingColumnNumber=0&amp;StartingLineNumber=35&amp;Timestamp=622808872.128901"
7+
selectedRepresentationIndex = "0"
8+
shouldTrackSuperviewWidth = "NO">
9+
</LoggerValueHistoryTimelineItem>
10+
</TimelineItems>
11+
</Timeline>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /