Skip to content

Commit

Permalink
feat: add swift implementation to lcof problem: No.27 (#2894)
Browse files Browse the repository at this point in the history
  • Loading branch information
klever34 committed May 23, 2024
1 parent 705c0f0 commit cd82966
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lcof/面试题27. 二叉树的镜像/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,36 @@ public class Solution {
}
```

#### Swift

```swift
/* public class TreeNode {
* var val: Int
* var left: TreeNode?
* var right: TreeNode?
* init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/

class Solution {
func mirrorTree(_ root: TreeNode?) -> TreeNode? {
guard let root = root else {
return nil
}
let temp = root.left
root.left = root.right
root.right = temp
_ = mirrorTree(root.left)
_ = mirrorTree(root.right)
return root
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
25 changes: 25 additions & 0 deletions lcof/面试题27. 二叉树的镜像/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* public class TreeNode {
* var val: Int
* var left: TreeNode?
* var right: TreeNode?
* init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/

class Solution {
func mirrorTree(_ root: TreeNode?) -> TreeNode? {
guard let root = root else {
return nil
}
let temp = root.left
root.left = root.right
root.right = temp
_ = mirrorTree(root.left)
_ = mirrorTree(root.right)
return root
}
}

0 comments on commit cd82966

Please sign in to comment.