#include <iostream>
using namespace std;
struct BiTreeNode//结点结构体
{
char ch;
BiTreeNode *left;
BiTreeNode *right;
BiTreeNode(char x, BiTreeNode *l = NULL, BiTreeNode *r = NULL)
:ch(x), left(l), right(r)
{
}
};
void insert(BiTreeNode *&root, char ch)//插入结点
{
if(root == NULL)
{
root = new BiTreeNode(ch, NULL, NULL);
}
else if(ch < root->ch)//插入左子树
{
insert(root->left, ch);
}
else //插入右子树
{
insert(root->right, ch);
}
}
bool match(BiTreeNode *&root, BiTreeNode *&temp)//判断两棵树是否相同
{
if(root == NULL && temp == NULL)
{
return true;
}
else if((root == NULL && temp != NULL) || (root != NULL && temp == NULL))
{
return false;
}
else if(root->ch == temp->ch && match(root->left, temp->left) && match(root->right, temp->right))
{
return true;
}
else
{
return false;
}
}
void makeEmpty(BiTreeNode *&root)//清空结点
{
if(root != NULL)
{
makeEmpty(root->left);
makeEmpty(root->right);
delete root;
}
root = NULL;
}
int main(void)
{
int n;
char ch;
BiTreeNode *root = NULL;
BiTreeNode *temp = NULL;
while(cin >> n && n != 0)
{
ch = cin.get();
ch = cin.get();
while(ch != '\n')
{
insert(root, ch);
ch = cin.get();
}
while(n--)
{
ch = cin.get();
while(ch != '\n')
{
insert(temp, ch);
ch = cin.get();
}
cout << (match(root, temp) ? "YES" : "NO") << endl;
makeEmpty(temp);
}
makeEmpty(root);
}
return 0;
}
hdoj 3791 二叉搜索树
最新推荐文章于 2019-08-30 18:38:00 发布