Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lcci/04.10.Check SubTree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,49 @@ impl Solution {
}
```

```swift
/* class TreeNode {
* var val: Int
* var left: TreeNode?
* var right: TreeNode?
*
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/

class Solution {
func checkSubTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
if t2 == nil {
return true
}
if t1 == nil {
return false
}
if isSameTree(t1, t2) {
return true
}
return checkSubTree(t1!.left, t2) || checkSubTree(t1!.right, t2)
}

private func isSameTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
if t1!.val != t2!.val {
return false
}
return isSameTree(t1!.left, t2!.left) && isSameTree(t1!.right, t2!.right)
}
}
```

<!-- tabs:end -->

<!-- end -->
45 changes: 44 additions & 1 deletion lcci/04.10.Check SubTree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ First, we check if $t_2$ is null. If it is, then $t_2$ is definitely a subtree o

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`.

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`.
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`.

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

Expand Down Expand Up @@ -276,6 +276,49 @@ impl Solution {
}
```

```swift
/* class TreeNode {
* var val: Int
* var left: TreeNode?
* var right: TreeNode?
*
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/

class Solution {
func checkSubTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
if t2 == nil {
return true
}
if t1 == nil {
return false
}
if isSameTree(t1, t2) {
return true
}
return checkSubTree(t1!.left, t2) || checkSubTree(t1!.right, t2)
}

private func isSameTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
if t1!.val != t2!.val {
return false
}
return isSameTree(t1!.left, t2!.left) && isSameTree(t1!.right, t2!.right)
}
}
```

<!-- tabs:end -->

<!-- end -->
40 changes: 40 additions & 0 deletions lcci/04.10.Check SubTree/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* class TreeNode {
* var val: Int
* var left: TreeNode?
* var right: TreeNode?
*
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/

class Solution {
func checkSubTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
if t2 == nil {
return true
}
if t1 == nil {
return false
}
if isSameTree(t1, t2) {
return true
}
return checkSubTree(t1!.left, t2) || checkSubTree(t1!.right, t2)
}

private func isSameTree(_ t1: TreeNode?, _ t2: TreeNode?) -> Bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
if t1!.val != t2!.val {
return false
}
return isSameTree(t1!.left, t2!.left) && isSameTree(t1!.right, t2!.right)
}
}