-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathleetcode-572-subtree-of-another-tree.swift
104 lines (76 loc) · 2.69 KB
/
leetcode-572-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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/subtree-of-another-tree/
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s:
3
/ \
4 5
/ \
1 2
Given tree t:
4
/ \
1 2
Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
3
/ \
4 5
/ \
1 2
/
0
Given tree t:
4
/ \
1 2
Return false.
*/
/**
Big O Annotation
Time complexity O(n) where n is the amount of nodes in s.
Space complexity O(1) we're not using any extra space.
*/
func isSubtree(_ s: TreeNode?, _ t: TreeNode?) -> Bool {
// Without no s tree, nothing to check
guard let s = s else {
return false
}
// Check if t is a subtree of s
guard getEquality(of: s, and: t) else {
// If not, continue on the left and on the right
return isSubtree(s.left, t) || isSubtree(s.right, t)
}
return true
}
private func getEquality(of s: TreeNode?, and t: TreeNode?) -> Bool {
// If either node is nil...
if s == nil || t == nil {
// ... both need to be nil
return s == nil && t == nil
// If the current nodes match
} else if s!.val == t!.val {
// Then all other children need to match as well
return getEquality(of: s!.left, and: t!.left) && getEquality(of: s!.right, and: t!.right)
}
// Otherwise it's not a subtree
return false
}