forked from ChenYangyao/project-leetcode-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem572.swift
195 lines (163 loc) · 5.58 KB
/
problem572.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import Dispatch
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 conversion {
func a_to_b(_ value: [Int?]) -> TreeNode? {
if (value[0] == nil) {
return nil
}
var deque = [TreeNode?]()
var index = 1
let output = TreeNode(value[0]!)
if (value.count == 1) {
return output
}
deque.append(output)
while (index < value.count) {
let tmp = deque.removeFirst()
if let val = value[index] {
tmp?.left = TreeNode(val)
deque.append(tmp?.left)
}
index += 1
if let val = value[index] {
tmp?.right = TreeNode(val)
deque.append(tmp?.right)
}
index += 1
}
return output
}
}
class Solution1 {
//define a class to store the number of nodes of left and right subtrees resp.
//class is passed as reference
private class countNumber {
var lNum: Int = 0
var rNum: Int = 0
}
private class stackItem: countNumber {
let p: TreeNode
var visited: Bool = false
var direction: Bool //true: right; false: left
var parent: countNumber
//we need to modify parent's countNumber class later
init(_ p: TreeNode, _ parent: countNumber, _ direction: Bool) {
self.p = p
self.parent = parent
self.direction = direction
}
}
//https://github.com/ChenYangyao/project-leetcode-solution/blob/master/swift/problem100.swift
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
return (p == nil && q == nil) || (p?.val == q?.val) && isSameTree(p!.right, q!.right) && isSameTree(p!.left, q!.left)
}
func nodeNumber(_ p: TreeNode?) -> Int {
if (p == nil) {
return 0
}
var stkNode = [TreeNode]()
var tmpNode = p!
var number = 0
stkNode.append(tmpNode)
while !stkNode.isEmpty {
tmpNode = stkNode.removeLast()
if (tmpNode.left != nil) {
stkNode.append(tmpNode.left!)
}
if (tmpNode.right != nil) {
stkNode.append(tmpNode.right!)
}
number += 1
}
return number
}
//we first determine the number of nodes with repect to root node p. If it is the same as t's, we then judge whether p and s are the same tree.
//we count the number of nodes w.r.t every nodes in s reversely using stack.
//swift implementation of cpp version.
//https://github.com/ChenYangyao/project-leetcode-solution/blob/master/cpp/problem572.cpp
func isSubtree(_ s: TreeNode?, _ t: TreeNode?) -> Bool {
if (s == nil) {
return (t == nil)
}
var stack = [stackItem]()
let tNum = nodeNumber(t)
stack.append(stackItem(s!, countNumber(), false))
//post-order traverse
while !stack.isEmpty {
let top = stack.last!
if top.visited {
let tmp = top.lNum + top.rNum + 1
if top.direction {
top.parent.rNum = tmp
} else {
top.parent.lNum = tmp
}
if (tmp == tNum && isSameTree(top.p,t)) {
return true
}
_ = stack.removeLast()
} else {
top.visited = true
if (top.p.left != nil) {
stack.append(stackItem(top.p.left!,top, false))
//passing subclass instance to superclass is allowed
}
if (top.p.right != nil) {
stack.append(stackItem(top.p.right!,top, true))
}
}
}
return false
}
}
//ref: sample 172 ms submission
class Solution2 {
func isSubtree(_ s: TreeNode?, _ t: TreeNode?) -> Bool {
return (s == nil && t == nil) || ( s != nil && t != nil && (isSameTree(s,t) || isSubtree(s!.left,t) || isSubtree(s!.right,t)))
}
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
return (p == nil && q == nil) || (p?.val == q?.val) && isSameTree(p!.right, q!.right) && isSameTree(p!.left, q!.left)
}
}
protocol sol {
func isSubtree(_ s: TreeNode?, _ t: TreeNode?) -> Bool
}
extension Solution1: sol{}
extension Solution2: sol{}
var sInput = [Int](repeating: 0, count: 16383)
var tInput = [Int](repeating: 0, count: 8191)
for i in 0..<16383 {
sInput[i] = Int.random(in: 0...100)
}
for i in 0..<8191 {
tInput[i] = Int.random(in: 0...100)
}
let s = conversion().a_to_b(sInput)
let t = conversion().a_to_b(tInput)
func time_consuming<T: sol>(_ solution_class: T, _ name: String, _ r: Int) {
let time_start = DispatchTime.now().uptimeNanoseconds
for _ in 1...r {
_ = solution_class.isSubtree(s,t)
}
let time_end = DispatchTime.now().uptimeNanoseconds
print(name,(time_end-time_start)/1000000,"ms","for \(r) times")
}
time_consuming(Solution1(),"optimised",100)
time_consuming(Solution2(),"brute force",100)
/*
brute force method is faster than optimised one on my mac =_=
optimised 692 ms for 100 times
brute force 433 ms for 100 times
but it is just the opposite on Leetcode's website.
128 ms vs 352 ms
I have no idea about this.
*/