Skip to content

Commit c3034e3

Browse files
authored
feat: add swift implementation to lcof problem: No.55.1 (#2941)
1 parent 1c10208 commit c3034e3

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lcof/面试题55 - I. 二叉树的深度/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,31 @@ public class Solution {
218218
}
219219
```
220220

221+
#### Swift
222+
223+
```swift
224+
/* public class TreeNode {
225+
* public var val: Int
226+
* public var left: TreeNode?
227+
* public var right: TreeNode?
228+
* public init(_ val: Int) {
229+
* self.val = val
230+
* self.left = nil
231+
* self.right = nil
232+
* }
233+
* }
234+
*/
235+
236+
class Solution {
237+
func maxDepth(_ root: TreeNode?) -> Int {
238+
guard let root = root else {
239+
return 0
240+
}
241+
return 1 + max(maxDepth(root.left), maxDepth(root.right))
242+
}
243+
}
244+
```
245+
221246
<!-- tabs:end -->
222247

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

0 commit comments

Comments
 (0)