From cd8296652471ea40e0e547430f596c19ec6e5fe7 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 23 May 2024 09:31:58 +0100 Subject: [PATCH] feat: add swift implementation to lcof problem: No.27 (#2894) --- .../README.md" | 30 +++++++++++++++++++ .../Solution.swift" | 25 ++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" index 31a1c1b43602..90c697d3bad0 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" @@ -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 + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.swift" new file mode 100644 index 000000000000..34a382175ac8 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.swift" @@ -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 + } +} \ No newline at end of file