Skip to content

Commit 9d76d65

Browse files
authored
feat: add swift implementation to lcci problem: No.04.10 (doocs#2661)
1 parent 210021b commit 9d76d65

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

lcci/04.10.Check SubTree/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,49 @@ impl Solution {
269269
}
270270
```
271271

272+
```swift
273+
/* class TreeNode {
274+
* var val: Int
275+
* var left: TreeNode?
276+
* var right: TreeNode?
277+
*
278+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
279+
* self.val = val
280+
* self.left = left
281+
* self.right = right
282+
* }
283+
* }
284+
*/
285+
286+
class Solution {
287+
func checkSubTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
288+
if t2 == nil {
289+
return true
290+
}
291+
if t1 == nil {
292+
return false
293+
}
294+
if isSameTree(t1, t2) {
295+
return true
296+
}
297+
return checkSubTree(t1!.left, t2) || checkSubTree(t1!.right, t2)
298+
}
299+
300+
private func isSameTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
301+
if t1 == nil && t2 == nil {
302+
return true
303+
}
304+
if t1 == nil || t2 == nil {
305+
return false
306+
}
307+
if t1!.val != t2!.val {
308+
return false
309+
}
310+
return isSameTree(t1!.left, t2!.left) && isSameTree(t1!.right, t2!.right)
311+
}
312+
}
313+
```
314+
272315
<!-- tabs:end -->
273316

274317
<!-- end -->

lcci/04.10.Check SubTree/README_EN.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ First, we check if $t_2$ is null. If it is, then $t_2$ is definitely a subtree o
4242

4343
Otherwise, we check if $t_1$ is null. If it is, then $t_2$ is definitely not a subtree of $t_1$, so we return `false`.
4444

45-
Next, we check if $t_1$ and $t_2$ are equal. If they are, then $t_2$ is a subtree of $t_1$, so we return `true`. Otherwise, we recursively check if $t_1$'s left subtree is equal to $t_2$, and if $t_1$'s right subtree is equal to $t_2$. If either is `true`, then $t_2$ is a subtree of $t_1`, so we return `true`.
45+
Next, we check if $t_1$ and $t_2$ are equal. If they are, then $t_2$ is a subtree of $t_1$, so we return `true`. Otherwise, we recursively check if $t_1$'s left subtree is equal to $t_2$, and if $t_1$'s right subtree is equal to $t_2$. If either is `true`, then $t_2$ is a subtree of $t_1$, so we return `true`.
4646

4747
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in $t_1$.
4848

@@ -276,6 +276,49 @@ impl Solution {
276276
}
277277
```
278278

279+
```swift
280+
/* class TreeNode {
281+
* var val: Int
282+
* var left: TreeNode?
283+
* var right: TreeNode?
284+
*
285+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
286+
* self.val = val
287+
* self.left = left
288+
* self.right = right
289+
* }
290+
* }
291+
*/
292+
293+
class Solution {
294+
func checkSubTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
295+
if t2 == nil {
296+
return true
297+
}
298+
if t1 == nil {
299+
return false
300+
}
301+
if isSameTree(t1, t2) {
302+
return true
303+
}
304+
return checkSubTree(t1!.left, t2) || checkSubTree(t1!.right, t2)
305+
}
306+
307+
private func isSameTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
308+
if t1 == nil && t2 == nil {
309+
return true
310+
}
311+
if t1 == nil || t2 == nil {
312+
return false
313+
}
314+
if t1!.val != t2!.val {
315+
return false
316+
}
317+
return isSameTree(t1!.left, t2!.left) && isSameTree(t1!.right, t2!.right)
318+
}
319+
}
320+
```
321+
279322
<!-- tabs:end -->
280323

281324
<!-- end -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* class TreeNode {
2+
* var val: Int
3+
* var left: TreeNode?
4+
* var right: TreeNode?
5+
*
6+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
7+
* self.val = val
8+
* self.left = left
9+
* self.right = right
10+
* }
11+
* }
12+
*/
13+
14+
class Solution {
15+
func checkSubTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
16+
if t2 == nil {
17+
return true
18+
}
19+
if t1 == nil {
20+
return false
21+
}
22+
if isSameTree(t1, t2) {
23+
return true
24+
}
25+
return checkSubTree(t1!.left, t2) || checkSubTree(t1!.right, t2)
26+
}
27+
28+
private func isSameTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
29+
if t1 == nil && t2 == nil {
30+
return true
31+
}
32+
if t1 == nil || t2 == nil {
33+
return false
34+
}
35+
if t1!.val != t2!.val {
36+
return false
37+
}
38+
return isSameTree(t1!.left, t2!.left) && isSameTree(t1!.right, t2!.right)
39+
}
40+
}

0 commit comments

Comments
 (0)