From 553f5d68e585688df6bcfbb2a35adcd80349ec92 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 28 May 2024 08:08:32 +0100 Subject: [PATCH] Swift implementation for LCOF 55-I --- .../README.md" | 25 +++++++++++++++++++ .../Solution.swift" | 20 +++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" index 7f9176a295456..adcb15bfdcc41 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" @@ -218,6 +218,31 @@ public class Solution { } ``` +#### Swift + +```swift +/* public class TreeNode { +* public var val: Int +* public var left: TreeNode? +* public var right: TreeNode? +* public init(_ val: Int) { +* self.val = val +* self.left = nil +* self.right = nil +* } +* } +*/ + +class Solution { + func maxDepth(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + return 1 + max(maxDepth(root.left), maxDepth(root.right)) + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.swift" new file mode 100644 index 0000000000000..efba7e1ff02fb --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.swift" @@ -0,0 +1,20 @@ +/* public class TreeNode { +* public var val: Int +* public var left: TreeNode? +* public var right: TreeNode? +* public init(_ val: Int) { +* self.val = val +* self.left = nil +* self.right = nil +* } +* } +*/ + +class Solution { + func maxDepth(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + return 1 + max(maxDepth(root.left), maxDepth(root.right)) + } +} \ No newline at end of file