-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
62 lines (51 loc) · 1.43 KB
/
Copy pathsolution.cpp
File metadata and controls
62 lines (51 loc) · 1.43 KB
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
/*
Definition for Node
struct Node
{
int data;
Node* left;
Node* right;
Node(int x){
data = x;
left = right = nullptr;
}
};
*/
class Solution
{
public:
// Function to check whether two trees are identical
bool isSame(Node *a, Node *b)
{
// If both nodes are NULL, trees match till here
if (a == NULL && b == NULL)
return true;
// If one node is NULL and the other is not
// then structure is different
if (a == NULL || b == NULL)
return false;
// If current node values are different
// then trees are not identical
if (a->data != b->data)
return false;
// Recursively check left and right subtrees
return isSame(a->left, b->left) &&
isSame(a->right, b->right);
}
bool isSubTree(Node *root1, Node *root2)
{
// Empty tree is always a subtree
if (root2 == NULL)
return true;
// If main tree becomes empty
// subtree cannot exist anymore
if (root1 == NULL)
return false;
// If both trees match completely from this node
if (isSame(root1, root2))
return true;
// Otherwise search in left and right subtree
return isSubTree(root1->left, root2) ||
isSubTree(root1->right, root2);
}
};