Skip to content

Commit e38dc6e

Browse files
committed
feat: add swift implementation to lcp problem: No.67
1 parent b901bf8 commit e38dc6e

File tree

4 files changed

+78
-39
lines changed

4 files changed

+78
-39
lines changed

lcp/LCP 67. 装饰树/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,48 @@ func expandBinaryTree(root *TreeNode) *TreeNode {
197197
}
198198
```
199199

200+
#### Swift
201+
202+
```swift
203+
/* class TreeNode {
204+
* var val: Int
205+
* var left: TreeNode?
206+
* var right: TreeNode?
207+
208+
* init() { self.val = 0; self.left = nil; self.right = nil }
209+
* init(_ val: Int) { self.val = val; self.left = nil; self.right = nil }
210+
* init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
211+
* self.val = val
212+
* self.left = left
213+
* self.right = right
214+
* }
215+
* }
216+
*/
217+
218+
class Solution {
219+
func expandBinaryTree(_ root: TreeNode?) -> TreeNode? {
220+
return dfs(root)
221+
}
222+
223+
private func dfs(_ root: TreeNode?) -> TreeNode? {
224+
guard let root = root else { return nil }
225+
226+
let leftChild = dfs(root.left)
227+
let rightChild = dfs(root.right)
228+
229+
if let leftChild = leftChild {
230+
root.left = TreeNode(-1, leftChild, nil)
231+
}
232+
if let rightChild = rightChild {
233+
root.right = TreeNode(-1, nil, rightChild)
234+
}
235+
236+
return root
237+
}
238+
}
239+
240+
```
241+
200242
<!-- tabs:end -->
201243

202244
<!-- solution:end -->

lcp/LCP 67. 装饰树/Solution.swift

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* class TreeNode {
2+
* var val: Int
3+
* var left: TreeNode?
4+
* var right: TreeNode?
5+
6+
* init() { self.val = 0; self.left = nil; self.right = nil }
7+
* init(_ val: Int) { self.val = val; self.left = nil; self.right = nil }
8+
* init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
9+
* self.val = val
10+
* self.left = left
11+
* self.right = right
12+
* }
13+
* }
14+
*/
15+
16+
class Solution {
17+
func expandBinaryTree(_ root: TreeNode?) -> TreeNode? {
18+
return dfs(root)
19+
}
20+
21+
private func dfs(_ root: TreeNode?) -> TreeNode? {
22+
guard let root = root else { return nil }
23+
24+
let leftChild = dfs(root.left)
25+
let rightChild = dfs(root.right)
26+
27+
if let leftChild = leftChild {
28+
root.left = TreeNode(-1, leftChild, nil)
29+
}
30+
if let rightChild = rightChild {
31+
root.right = TreeNode(-1, nil, rightChild)
32+
}
33+
34+
return root
35+
}
36+
}

solution/1500-1599/1535.Find the Winner of an Array Game/Solution.cs

-17
This file was deleted.

solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.cs

-22
This file was deleted.

0 commit comments

Comments
 (0)