Skip to content

Commit ca67b79

Browse files
authored
feat: add swift implementation to lcci problem: No.04.09 (doocs#2660)
1 parent 1e45282 commit ca67b79

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

lcci/04.09.BST Sequences/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,61 @@
2121

2222
## 解法
2323

24+
<!-- tabs:start -->
25+
26+
```swift
27+
/* class TreeNode {
28+
* var val: Int
29+
* var left: TreeNode?
30+
* var right: TreeNode?
31+
*
32+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
33+
* self.val = val
34+
* self.left = left
35+
* self.right = right
36+
* }
37+
* }
38+
*/
39+
40+
class Solution {
41+
func BSTSequences(_ root: TreeNode?) -> [[Int]] {
42+
guard let root = root else { return [[]] }
43+
44+
var result = [[Int]]()
45+
let prefix = [root.val]
46+
let leftSeq = BSTSequences(root.left)
47+
let rightSeq = BSTSequences(root.right)
48+
49+
for left in leftSeq {
50+
for right in rightSeq {
51+
var weaved = [[Int]]()
52+
weaveLists(left, right, &weaved, prefix)
53+
result.append(contentsOf: weaved)
54+
}
55+
}
56+
return result
57+
}
58+
59+
private func weaveLists(_ first: [Int], _ second: [Int], _ results: inout [[Int]], _ prefix: [Int]) {
60+
if first.isEmpty || second.isEmpty {
61+
var result = prefix
62+
result.append(contentsOf: first)
63+
result.append(contentsOf: second)
64+
results.append(result)
65+
return
66+
}
67+
68+
var prefixWithFirst = prefix
69+
prefixWithFirst.append(first.first!)
70+
weaveLists(Array(first.dropFirst()), second, &results, prefixWithFirst)
71+
72+
var prefixWithSecond = prefix
73+
prefixWithSecond.append(second.first!)
74+
weaveLists(first, Array(second.dropFirst()), &results, prefixWithSecond)
75+
}
76+
}
77+
```
78+
79+
<!-- tabs:end -->
80+
2481
<!-- end -->

lcci/04.09.BST Sequences/README_EN.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,61 @@ Given the following tree:</p>
3131

3232
## Solutions
3333

34+
<!-- tabs:start -->
35+
36+
```swift
37+
/* class TreeNode {
38+
* var val: Int
39+
* var left: TreeNode?
40+
* var right: TreeNode?
41+
*
42+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
43+
* self.val = val
44+
* self.left = left
45+
* self.right = right
46+
* }
47+
* }
48+
*/
49+
50+
class Solution {
51+
func BSTSequences(_ root: TreeNode?) -> [[Int]] {
52+
guard let root = root else { return [[]] }
53+
54+
var result = [[Int]]()
55+
let prefix = [root.val]
56+
let leftSeq = BSTSequences(root.left)
57+
let rightSeq = BSTSequences(root.right)
58+
59+
for left in leftSeq {
60+
for right in rightSeq {
61+
var weaved = [[Int]]()
62+
weaveLists(left, right, &weaved, prefix)
63+
result.append(contentsOf: weaved)
64+
}
65+
}
66+
return result
67+
}
68+
69+
private func weaveLists(_ first: [Int], _ second: [Int], _ results: inout [[Int]], _ prefix: [Int]) {
70+
if first.isEmpty || second.isEmpty {
71+
var result = prefix
72+
result.append(contentsOf: first)
73+
result.append(contentsOf: second)
74+
results.append(result)
75+
return
76+
}
77+
78+
var prefixWithFirst = prefix
79+
prefixWithFirst.append(first.first!)
80+
weaveLists(Array(first.dropFirst()), second, &results, prefixWithFirst)
81+
82+
var prefixWithSecond = prefix
83+
prefixWithSecond.append(second.first!)
84+
weaveLists(first, Array(second.dropFirst()), &results, prefixWithSecond)
85+
}
86+
}
87+
```
88+
89+
<!-- tabs:end -->
90+
3491
<!-- end -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* class TreeNode {
2+
* var val: Int
3+
* var left: TreeNode?
4+
* var right: TreeNode?
5+
*
6+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
7+
* self.val = val
8+
* self.left = left
9+
* self.right = right
10+
* }
11+
* }
12+
*/
13+
14+
class Solution {
15+
func BSTSequences(_ root: TreeNode?) -> [[Int]] {
16+
guard let root = root else { return [[]] }
17+
18+
var result = [[Int]]()
19+
let prefix = [root.val]
20+
let leftSeq = BSTSequences(root.left)
21+
let rightSeq = BSTSequences(root.right)
22+
23+
for left in leftSeq {
24+
for right in rightSeq {
25+
var weaved = [[Int]]()
26+
weaveLists(left, right, &weaved, prefix)
27+
result.append(contentsOf: weaved)
28+
}
29+
}
30+
return result
31+
}
32+
33+
private func weaveLists(_ first: [Int], _ second: [Int], _ results: inout [[Int]], _ prefix: [Int]) {
34+
if first.isEmpty || second.isEmpty {
35+
var result = prefix
36+
result.append(contentsOf: first)
37+
result.append(contentsOf: second)
38+
results.append(result)
39+
return
40+
}
41+
42+
var prefixWithFirst = prefix
43+
prefixWithFirst.append(first.first!)
44+
weaveLists(Array(first.dropFirst()), second, &results, prefixWithFirst)
45+
46+
var prefixWithSecond = prefix
47+
prefixWithSecond.append(second.first!)
48+
weaveLists(first, Array(second.dropFirst()), &results, prefixWithSecond)
49+
}
50+
}

0 commit comments

Comments
 (0)