8000 Finished 111. · lexrus/LeetCode.swift@b14b3a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b14b3a3

Browse files
committed
Finished 111.
1 parent 5d696b4 commit b14b3a3

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

LeetCode.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
D738487E1B301E6E00B734C9 /* 101.swift in Sources */ = {isa = PBXBuildFile; fileRef = D738487D1B301E6E00B734C9 /* 101.swift */; };
1616
D73A13E31B9C6FDB001579E8 /* 258.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A13E21B9C6FDB001579E8 /* 258.swift */; };
1717
D73A13E51B9C85C8001579E8 /* 263.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A13E41B9C85C8001579E8 /* 263.swift */; };
18+
D743F3DC1C2EE7BF00A7BC6E /* 111.swift in Sources */ = {isa = PBXBuildFile; fileRef = D743F3DB1C2EE7BF00A7BC6E /* 111.swift */; };
1819
D7803DC21B95F1FE000C9270 /* 104.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7803DC11B95F1FE000C9270 /* 104.swift */; };
1920
D783CC9B1B2AC9160087FFC1 /* 226.swift in Sources */ = {isa = PBXBuildFile; fileRef = D783CC9A1B2AC9160087FFC1 /* 226.swift */; };
2021
D7AB14CB1B99C85200DEDE9D /* 58.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7AB14CA1B99C85200DEDE9D /* 58.swift */; };
@@ -53,6 +54,7 @@
5354
D738487D1B301E6E00B734C9 /* 101.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 101.swift; sourceTree = "<group>"; };
5455
D73A13E21B9C6FDB001579E8 /* 258.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 258.swift; sourceTree = "<group>"; };
5556
D73A13E41B9C85C8001579E8 /* 263.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 263.swift; sourceTree = "<group>"; };
57+
D743F3DB1C2EE7BF00A7BC6E /* 111.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 111.swift; sourceTree = "<group>"; };
5658
D7803DC11B95F1FE000C9270 /* 104.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 104.swift; sourceTree = "<group>"; };
5759
D783CC9A1B2AC9160087FFC1 /* 226.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 226.swift; sourceTree = "<group>"; };
5860
D7AB14CA1B99C85200DEDE9D /* 58.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 58.swift; sourceTree = "<group>"; };
@@ -143,6 +145,7 @@
143145
D7DE016A1AF9F13600BB2AA0 /* 100.swift */,
144146
D738487D1B301E6E00B734C9 /* 101.swift */,
145147
D7803DC11B95F1FE000C9270 /* 104.swift */,
148+
D743F3DB1C2EE7BF00A7BC6E /* 111.swift */,
146149
D71CE8401B031E9A00D8A35F /* 165.swift */,
147150
D783CC9A1B2AC9160087FFC1 /* 226.swift */,
148151
D70B52A01C291563007E16DC /* 242.swift */,
@@ -271,6 +274,7 @@
271274
D7FB8E281AF89ADB00286272 /* 7.swift in Sources */,
272275
D7FB8E1A1AF89A6500286272 /* 14.swift in Sources */,
273276
D7FB8E6A1AF9B9D400286272 /* 82.swift in Sources */,
277+
D743F3DC1C2EE7BF00A7BC6E /* 111.swift in Sources */,
274278
D738487E1B301E6E00B734C9 /* 101.swift in Sources */,
275279
D7FB8E2A1AF89AE500286272 /* 20.swift in Sources */,
276280
D71563861AF9BF780090A569 /* TreeNode.swift in Sources */,

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Travis](https://img.shields.io/travis/lexrus/LeetCode.swift.svg)](https://travis-ci.org/lexrus/LeetCode.swift)
44
![Language](https://img.shields.io/badge/language-Swift%202-orange.svg)
55
![License](https://img.shields.io/github/license/lexrus/ios-dev-playbook.svg?style=flat)
6-
![Progress](https://img.shields.io/badge/Progress-31%20%2F%20303%20=%2010.23%25-ff69b4.svg)
6+
![Progress](https://img.shields.io/badge/Progress-32%20%2F%20304%20=%2010.53%25-ff69b4.svg)
77

88
> 愚者千虑,必有一得。
99
@@ -125,7 +125,7 @@ let 😎 = Easy, 😐 = Medium, 😨 = Hard
125125
- [ ] 108 😐 [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) [.swift](./Tests/108.swift)
126126
- [ ] 109 😐 [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) [.swift](./Tests/109.swift)
127127
- [ ] 110 😎 [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) [.swift](./Tests/110.swift)
128-
- [ ] 111 😎 [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) [.swift](./Tests/111.swift)
128+
- [x] 111 😎 [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) [.swift](./Tests/111.swift)
129129
- [ ] 112 😎 [Path Sum](https://leetcode.com/problems/path-sum/) [.swift](./Tests/112.swift)
130130
- [ ] 113 😐 [Path Sum II](https://leetcode.com/problems/path-sum-ii/) [.swift](./Tests/113.swift)
131131
- [ ] 114 😐 [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) [.swift](./Tests/114.swift)
@@ -318,6 +318,7 @@ let 😎 = Easy, 😐 = Medium, 😨 = Hard
318318
- [ ] 318 😐 [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) [.swift](./Tests/318.swift)
319319
- [ ] 319 😐 [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) [.swift](./Tests/319.swift)
320320
- [ ] 320 😐 [Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/) [.swift](./Tests/320.swift) ㊙️
321+
- [ ] 321 😨 [Create Maximum Number](https://leetcode.com/problems/create-maximum-number/) [.swift](./Tests/321.swift)
321322

322323

323324
## Unit tests

Tests/111.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// 111.swift
3+
// LeetCode
4+
//
5+
// Created by Lex on 12/26/15.
6+
// Copyright © 2015 Lex Tang. All rights reserved.
7+
//
8+
9+
/*
10+
Given a binary tree, find its minimum depth.
11+
12+
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
13+
*/
14+
15+
import Foundation
16+
import XCTest
17+
18+
extension TreeNode {
19+
20+
private func minimumDepth(node: TreeNode?) -> Int {
21+
if node == nil {
22+
return 0
23+
}
24+
if let left = node?.leftNode, right = node?.rightNode {
25+
return min(minimumDepth(left), minimumDepth(right)) + 1
26+
}
27+
return max(minimumDepth(node?.leftNode), minimumDepth(node?.rightNode)) + 1
28+
}
29+
30+
var minimumDepth: Int {
31+
return minimumDepth(self)
32+
}
33+
34+
}
35+
36+
class MinimumDepthTest: XCTestCase {
37+
38+
func testMinimumDepth() {
39+
XCTAssert(TreeNode(0).minimumDepth == 1)
40+
XCTAssert(TreeNode(0, TreeNode(1, TreeNode(2))).minimumDepth == 3)
41+
XCTAssert(TreeNode(0, TreeNode(1, TreeNode(2)), TreeNode(0)).minimumDepth == 2)
42+
}
43+
44+
}

0 commit comments

Comments
 (0)
0