Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 2a49d23

Browse files
committed
111. Minimum Depth of Binary Tree
1 parent f4df3a1 commit 2a49d23

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

111. Minimum Depth of Binary Tree.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 111. Minimum Depth of Binary Tree
2+
3+
### 2017-03-16
4+
5+
Given a binary tree, find its minimum depth.
6+
7+
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
8+
9+
10+
11+
# Solution
12+
13+
```swift
14+
/**
15+
* Definition for a binary tree node.
16+
* public class TreeNode {
17+
* public var val: Int
18+
* public var left: TreeNode?
19+
* public var right: TreeNode?
20+
* public init(_ val: Int) {
21+
* self.val = val
22+
* self.left = nil
23+
* self.right = nil
24+
* }
25+
* }
26+
*/
27+
class Solution {
28+
func minDepth(_ root: TreeNode?) -> Int {
29+
guard let r = root else { return 0 }
30+
var temp = [root]
31+
var deep = 0
32+
while temp.count != 0 {
33+
deep += 1
34+
for _ in 0..<temp.count {
35+
guard let n = temp.removeFirst() else { return deep }
36+
if n.left == nil && n.right == nil { return deep }
37+
if let l = n.left { temp.append(l) }
38+
if let r = n.right { temp.append(r) }
39+
}
40+
}
41+
return deep
42+
}
43+
}
44+
```
45+

0 commit comments

Comments
 (0)