-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtree_of_another_tree.cpp
55 lines (49 loc) · 1.4 KB
/
subtree_of_another_tree.cpp
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
// so i iterate through each subtree here calling the checkSameTree method to figure out whether there
// exists a portion where they are the same
bool isSubtree(TreeNode *s, TreeNode *t)
{
// if it does not exist
if (!s)
{
return false;
}
//first if they're the exact same
if (checkSameTree(s, t))
{
return true;
}
// same for the left and right
return (isSubtree(s->left, t) || isSubtree(s->right, t));
}
// so this method will go through each subtree passed to it to determine if they are the same
bool checkSameTree(TreeNode *s, TreeNode *t)
{
// both empty means that they are the same
if (s == nullptr && t == nullptr)
{
return true;
}
// but one being not empty when other is = nto same
if (s == nullptr || t == nullptr) // alteratively s = null but t != null || t== null but s != null
{
return false;
}
if (s->val != t->val)
{
return false;
}
return checkSameTree(s->left, t->left) && checkSameTree(s->right, t->right);
}
};