forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0572-subtree-of-another-tree.swift
48 lines (43 loc) · 1.58 KB
/
0572-subtree-of-another-tree.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
private func areSameTrees(
_ node1: TreeNode?,
_ node2: TreeNode?
) -> Bool {
// if both are nil, they are same
if node1 == nil && node2 == nil { return true }
// if anyone else is nil, they are not the same
guard let node1 = node1 else { return false }
guard let node2 = node2 else { return false }
return (
(node1.val == node2.val) &&
areSameTrees(node1.left, node2.left) &&
areSameTrees(node1.right, node2.right)
)
}
func isSubtree(_ root: TreeNode?, _ subRoot: TreeNode?) -> Bool {
// Base cases (both can be nil, just any one can't)
if root == nil && subRoot == nil { return true }
guard let root = root else { return false }
guard let subRoot = subRoot else { return false }
// Base case: Exactly same tree
if areSameTrees(root, subRoot) { return true }
// Recursive
return isSubtree(root.left, subRoot)
|| isSubtree(root.right, subRoot)
}
}