Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e45282

Browse files
feat: add swift implementation to lcci problem: No.04.08 (doocs#2659)
1 parent 5c82aa3 commit 1e45282

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

‎lcci/04.08.First Common Ancestor/README.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ class Solution {
5555
}
5656
```
5757

58+
```swift
59+
/* class TreeNode {
60+
* var val: Int
61+
* var left: TreeNode?
62+
* var right: TreeNode?
63+
*
64+
* init(_ val: Int) {
65+
* self.val = val
66+
* self.left = nil
67+
* self.right = nil
68+
* }
69+
* }
70+
*/
71+
72+
class Solution {
73+
func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? {
74+
if root == nil || root === p || root === q {
75+
return root
76+
}
77+
let left = lowestCommonAncestor(root?.left, p, q)
78+
let right = lowestCommonAncestor(root?.right, p, q)
79+
if left == nil {
80+
return right
81+
} else if right == nil {
82+
return left
83+
} else {
84+
return root
85+
}
86+
}
87+
}
88+
```
89+
5890
<!-- tabs:end -->
5991

6092
<!-- end -->

‎lcci/04.08.First Common Ancestor/README_EN.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,38 @@ class Solution {
101101
}
102102
```
103103

104+
```swift
105+
/* class TreeNode {
106+
* var val: Int
107+
* var left: TreeNode?
108+
* var right: TreeNode?
109+
*
110+
* init(_ val: Int) {
111+
* self.val = val
112+
* self.left = nil
113+
* self.right = nil
114+
* }
115+
* }
116+
*/
117+
118+
class Solution {
119+
func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? {
120+
if root == nil || root === p || root === q {
121+
return root
122+
}
123+
let left = lowestCommonAncestor(root?.left, p, q)
124+
let right = lowestCommonAncestor(root?.right, p, q)
125+
if left == nil {
126+
return right
127+
} else if right == nil {
128+
return left
129+
} else {
130+
return root
131+
}
132+
}
133+
}
134+
```
135+
104136
<!-- tabs:end -->
105137

106138
<!-- end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* class TreeNode {
2+
* var val: Int
3+
* var left: TreeNode?
4+
* var right: TreeNode?
5+
*
6+
* init(_ val: Int) {
7+
* self.val = val
8+
* self.left = nil
9+
* self.right = nil
10+
* }
11+
* }
12+
*/
13+
14+
class Solution {
15+
func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? {
16+
if root == nil || root === p || root === q {
17+
return root
18+
}
19+
let left = lowestCommonAncestor(root?.left, p, q)
20+
let right = lowestCommonAncestor(root?.right, p, q)
21+
if left == nil {
22+
return right
23+
} else if right == nil {
24+
return left
25+
} else {
26+
return root
27+
}
28+
}
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /