-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subtree of another tree
59 lines (52 loc) · 1.08 KB
/
Subtree of another tree
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
/***************************************************
Following is the TreeNode Structure
template <typename T>
class TreeNode {
public:
T val;
TreeNode<T>* left;
TreeNode<T>* right;
TreeNode(T val) {
this->val = val;
left = NULL;
right = NULL;
}
};
*****************************************************/
bool issame(TreeNode<int> *T, TreeNode<int> *S)
{
if(T == NULL && S == NULL)
{
return true ;
}
else if(T == NULL || S == NULL){
return false;
}
if(T->val != S->val)
{
return false ;
}
bool a = issame(T->left , S->left) ;
bool b = issame(T->right , S->right) ;
return a && b ;
}
bool isSubtree(TreeNode<int> *T, TreeNode<int> *S)
{
if(T == NULL)
{
return false ;
}
if(T->val == S->val )
{
bool x = issame(T , S) ;
if(x == true)
{
return true ;
}
}
bool a = isSubtree(T->left , S) ;
bool b = isSubtree(T->right , S
) ;
return a || b ;
// Write your code here.
}