-
Notifications
You must be signed in to change notification settings - Fork 6
/
18_SubstructureInTree.cpp
159 lines (139 loc) · 2.82 KB
/
18_SubstructureInTree.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
struct BinaryTreeNode
{
int value;
BinaryTreeNode *left;
BinaryTreeNode *right;
BinaryTreeNode(int val) {
value = val;
left = NULL;
right = NULL;
}
};
void CreateTree(BinaryTreeNode **root)
{
BinaryTreeNode *pNode;
queue<BinaryTreeNode *> Bqueue;
int val;
if(cin>>val && val != -1) {
*root = new BinaryTreeNode(val);
Bqueue.push(*root);
}
else {
*root = NULL;
return;
}
while(!Bqueue.empty()) {
pNode = Bqueue.front();
Bqueue.pop();
if(cin>>val) {
if(val != -1) {
pNode->left = new BinaryTreeNode(val);
Bqueue.push(pNode->left);
}
}
else
break;
if(cin>>val) {
if(val != -1) {
pNode->right = new BinaryTreeNode(val);
Bqueue.push(pNode->right);
}
}
else
break;
}
}
int InOrderRecursion(BinaryTreeNode *root)
{
if (root == NULL)
return 0;
InOrderRecursion(root->left);
cout << root->value;
InOrderRecursion(root->right);
return 0;
}
int InOrderTraverse(BinaryTreeNode *root)
{
stack<BinaryTreeNode *> stacks;
BinaryTreeNode *index = root;
while (index || !stacks.empty())
{
if (index)
{
stacks.push(index);
index = index->left;
}
else
{
index = stacks.top();
stacks.pop();
if (index == NULL)
return -1;
cout << index->value;
index = index->right;
}
}
return 0;
}
int DeleteTree(BinaryTreeNode **root)
{
if (*root == NULL)
return 0;
if ((*root)->left != NULL)
DeleteTree(&((*root)->left));
if ((*root)->right != NULL)
DeleteTree(&((*root)->right));
delete *root;
return 0;
}
bool DoesTree1HaveTree2(BinaryTreeNode *root1, BinaryTreeNode *root2)
{
// first to judge root2, then judge root1.
//when they are NULL at the same time, exchange them will be wrong!
if (root2 == NULL)
return true;
if (root1 == NULL)
return false;
if (root1->value != root2->value)
return false;
return DoesTree1HaveTree2(root1->left, root2->left) &&
DoesTree1HaveTree2(root1->right, root2->right);
}
bool HasSubtree(BinaryTreeNode *root1, BinaryTreeNode *root2)
{
bool result = false;
if (root1 != NULL && root2 != NULL)
{
if (root1->value == root2->value)
result = DoesTree1HaveTree2(root1, root2);
if (!result)
result = HasSubtree(root1->left, root2);
if (!result)
result = HasSubtree(root1->right, root2);
}
return result;
}
int main(void)
{
bool TF;
BinaryTreeNode *root1 = NULL, *root2 = NULL;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
CreateTree(&root1);
CreateTree(&root2);
InOrderTraverse(root1);
InOrderTraverse(root2);
TF = HasSubtree(root1, root2);
if (TF == true)
cout << "YES" << endl;
else
cout << "NO" << endl;
DeleteTree(&root1);
DeleteTree(&root2);
root1 = root2 = NULL;
return 0;
}