-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_SubstructureInTree.cpp
More file actions
128 lines (114 loc) · 4.21 KB
/
Copy path18_SubstructureInTree.cpp
File metadata and controls
128 lines (114 loc) · 4.21 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
bool IsSubtree(BinaryTreeNode* aRoot, BinaryTreeNode* bRoot)
{
if (bRoot == NULL)
return true;
if (aRoot == NULL)
return false;
if (aRoot->m_nValue == bRoot->m_nValue)
return IsSubtree(aRoot->m_pLeft, bRoot->m_pLeft) && IsSubtree(aRoot->m_pRight, bRoot->m_pRight);
return false;
}
bool HasSubtree(BinaryTreeNode* aRoot, BinaryTreeNode* bRoot)
{
if (aRoot == NULL && bRoot == NULL)
return true;
if (aRoot == NULL || bRoot == NULL)
return false;
if (aRoot->m_nValue == bRoot->m_nValue)
{
bool res = IsSubtree(aRoot->m_pLeft, bRoot->m_pLeft) && IsSubtree(aRoot->m_pRight, bRoot->m_pRight);
if (res)
return true;
}
return HasSubtree(aRoot->m_pLeft, bRoot) || HasSubtree(aRoot->m_pRight, bRoot);
}
int main()
{
BinaryTreeNode* root1 = new BinaryTreeNode;
root1->m_nValue = 8;
root1->m_pLeft = new BinaryTreeNode;
root1->m_pLeft->m_nValue = 8;
root1->m_pRight = new BinaryTreeNode;
root1->m_pRight->m_nValue = 7;
root1->m_pRight->m_pLeft = NULL;
root1->m_pRight->m_pRight = NULL;
root1->m_pLeft->m_pLeft = new BinaryTreeNode;
root1->m_pLeft->m_pLeft->m_nValue = 9;
root1->m_pLeft->m_pLeft->m_pLeft = NULL;
root1->m_pLeft->m_pLeft->m_pRight = NULL;
root1->m_pLeft->m_pRight = new BinaryTreeNode;
root1->m_pLeft->m_pRight->m_nValue = 2;
root1->m_pLeft->m_pRight->m_pLeft = new BinaryTreeNode;
root1->m_pLeft->m_pRight->m_pLeft->m_nValue = 4;
root1->m_pLeft->m_pRight->m_pLeft->m_pLeft = NULL;
root1->m_pLeft->m_pRight->m_pLeft->m_pRight = NULL;
root1->m_pLeft->m_pRight->m_pRight = new BinaryTreeNode;
root1->m_pLeft->m_pRight->m_pRight->m_nValue = 7;
root1->m_pLeft->m_pRight->m_pRight->m_pLeft = NULL;
root1->m_pLeft->m_pRight->m_pRight->m_pRight = NULL;
BinaryTreeNode* root2 = new BinaryTreeNode;
root2->m_nValue = 8;
root2->m_pLeft = new BinaryTreeNode;
root2->m_pLeft->m_nValue = 9;
root2->m_pLeft->m_pLeft = NULL;
root2->m_pLeft->m_pRight = NULL;
root2->m_pRight = new BinaryTreeNode;
root2->m_pRight->m_nValue = 2;
root2->m_pRight->m_pLeft = NULL;
root2->m_pRight->m_pRight = NULL;
cout << boolalpha << HasSubtree(root1, root2) << endl;
BinaryTreeNode* root3 = new BinaryTreeNode;
root3->m_nValue = 8;
root3->m_pLeft = new BinaryTreeNode;
root3->m_pLeft->m_nValue = 8;
root3->m_pRight = new BinaryTreeNode;
root3->m_pRight->m_nValue = 7;
root3->m_pRight->m_pLeft = NULL;
root3->m_pRight->m_pRight = NULL;
root3->m_pLeft->m_pLeft = new BinaryTreeNode;
root3->m_pLeft->m_pLeft->m_nValue = 9;
root3->m_pLeft->m_pLeft->m_pLeft = NULL;
root3->m_pLeft->m_pLeft->m_pRight = NULL;
root3->m_pLeft->m_pRight = new BinaryTreeNode;
root3->m_pLeft->m_pRight->m_nValue = 2;
root3->m_pLeft->m_pRight->m_pLeft = NULL;
root3->m_pLeft->m_pRight->m_pRight = NULL;
cout << HasSubtree(root3, root2) << endl;
BinaryTreeNode* root4 = new BinaryTreeNode;
root4->m_nValue = 8;
root4->m_pLeft = new BinaryTreeNode;
root4->m_pLeft->m_nValue = 9;
root4->m_pRight = new BinaryTreeNode;
root4->m_pRight->m_nValue = 2;
root4->m_pRight->m_pLeft = NULL;
root4->m_pRight->m_pRight = NULL;
root4->m_pLeft->m_pLeft = new BinaryTreeNode;
root4->m_pLeft->m_pLeft->m_nValue = 4;
root4->m_pLeft->m_pLeft->m_pLeft = NULL;
root4->m_pLeft->m_pLeft->m_pRight = NULL;
root4->m_pLeft->m_pRight = new BinaryTreeNode;
root4->m_pLeft->m_pRight->m_nValue = 7;
root4->m_pLeft->m_pRight->m_pLeft = NULL;
root4->m_pLeft->m_pRight->m_pRight = NULL;
cout << HasSubtree(root4, root2) << endl;
BinaryTreeNode* root5 = new BinaryTreeNode;
root5->m_nValue = 8;
root5->m_pLeft = new BinaryTreeNode;
root5->m_pLeft->m_nValue = 9;
root5->m_pLeft->m_pLeft = NULL;
root5->m_pLeft->m_pRight = NULL;
root5->m_pRight = new BinaryTreeNode;
root5->m_pRight->m_nValue = 7;
root5->m_pRight->m_pLeft = NULL;
root5->m_pRight->m_pRight = NULL;
cout << HasSubtree(root5, root2) << endl;
return 0;
}