# 1920 Problem B 二叉搜索树

[献花][花圈][TK题库]

6
45021
12045
54120
45021
45012
21054
50412
0

NO
NO
YES
NO
NO
NO

``````#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

typedef struct tnode
{
char v;
struct tnode * lchild;
struct tnode * rchild;
}TNode;

void Insert(TNode *&Root,int val)
{
if(!Root)
{
Root = new TNode;
Root->v = val;
Root->lchild = Root->rchild = NULL;
return;
}

if(Root->v == val) return;
else if(Root->v > val)
return Insert(Root->lchild,val);
else
return Insert(Root->rchild,val);
}

TNode * CreateTree(string seq)
{
TNode * root = NULL;
for(int i=0;i<seq.size();++i)
Insert(root,seq[i]);

return root;
}

void DestroyTree(TNode * &Root)
{
if(!Root)return;
DestroyTree(Root->lchild);
DestroyTree(Root->rchild);
delete Root;
Root = NULL;
}

void CheckSame(TNode * r1,TNode * r2,bool &flag)
{
if(!r1 && !r2) return;
if(!r1 && r2 || r1 && !r2)
{
flag = false;
return;
}

if(r1->v != r2->v)
{
flag = false;
return;
}

if(!flag)return;
CheckSame(r1->lchild,r2->lchild,flag);

if(!flag)return;
CheckSame(r1->rchild,r2->rchild,flag);
}

int main()
{
#ifdef _Debug
freopen("data.txt","r+",stdin);
#endif

std::ios::sync_with_stdio(false);

int n,tmp;
string seq,seq1;

while(cin >> n)
{
cin >> seq;
TNode *root = CreateTree(seq);

for(int i=0;i<n;++i)
{
cin >>seq1;
TNode *check = CreateTree(seq1);

bool flag = true;
CheckSame(root,check,flag);

if(flag)
cout << "YES"<<endl;
else
cout << "NO"<<endl;
}

}

return 0;
}

/************************************************************** Problem: 1920 User: Sharwen Language: C++ Result: 升仙 Time:2 ms Memory:1696 kb ****************************************************************/``````
原文作者：B树
原文地址: https://blog.csdn.net/fantasydreams/article/details/79340352
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。