Skip to content

Commit cd82966

Browse files
authored
feat: add swift implementation to lcof problem: No.27 (doocs#2894)
1 parent 705c0f0 commit cd82966

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lcof/面试题27. 二叉树的镜像/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,36 @@ public class Solution {
281281
}
282282
```
283283

284+
#### Swift
285+
286+
```swift
287+
/* public class TreeNode {
288+
* var val: Int
289+
* var left: TreeNode?
290+
* var right: TreeNode?
291+
* init(_ val: Int) {
292+
* self.val = val
293+
* self.left = nil
294+
* self.right = nil
295+
* }
296+
* }
297+
*/
298+
299+
class Solution {
300+
func mirrorTree(_ root: TreeNode?) -> TreeNode? {
301+
guard let root = root else {
302+
return nil
303+
}
304+
let temp = root.left
305+
root.left = root.right
306+
root.right = temp
307+
_ = mirrorTree(root.left)
308+
_ = mirrorTree(root.right)
309+
return root
310+
}
311+
}
312+
```
313+
284314
<!-- tabs:end -->
285315

286316
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* public class TreeNode {
2+
* var val: Int
3+
* var left: TreeNode?
4+
* var right: TreeNode?
5+
* init(_ val: Int) {
6+
* self.val = val
7+
* self.left = nil
8+
* self.right = nil
9+
* }
10+
* }
11+
*/
12+
13+
class Solution {
14+
func mirrorTree(_ root: TreeNode?) -> TreeNode? {
15+
guard let root = root else {
16+
return nil
17+
}
18+
let temp = root.left
19+
root.left = root.right
20+
root.right = temp
21+
_ = mirrorTree(root.left)
22+
_ = mirrorTree(root.right)
23+
return root
24+
}
25+
}

0 commit comments

Comments
 (0)