您的位置:首页 > 其它

是否同一棵二叉搜索树

2017-10-01 09:51 239 查看
04-树4 是否同一棵二叉搜索树

#include<stdio.h>
#include<stdlib.h>
#include<cstdio>
#include<iostream>
#include<stack>
#include<queue>
#include<string.h>
using namespace std;
#define N 100
int m,n,ans;
typedef struct BiNode
{
int name;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;

BiTree Insert(int a,BiTree Tst)
{
if(Tst==NULL)
{
Tst = new BiNode;
Tst->name=a;
Tst->lchild=NULL;
Tst->rchild=NULL;
}
else if(a<Tst->name)
{
Tst->lchild=Insert(a,Tst->lchild);
}
else
{
Tst->rchild=Insert(a,Tst->rchild);
}
return Tst;

}
BiTree InputTree(int m)
{
BiTree Tst=NULL;
for(int i=1;i<=m;i++)
{
int a;
scanf("%d",&a);
Tst=Insert(a,Tst);
}
return Tst;

}

void SolTree(BiTree Ast,BiTree Bst)
{

if(Ast!=NULL&&Bst!=NULL)
{
if(Ast->name==Bst->name)
ans++;
SolTree(Ast->lchild,Bst->lchild);
SolTree(Ast->rchild,Bst->rchild);
}

}
int main()
{

BiTree Ast=NULL,Bst=NULL;
while(scanf("%d",&m)&&m)
{
scanf("%d",&n);
Ast=InputTree(m);
while(n--)
{
Bst=InputTree(m);
ans=0;
SolTree(Ast,Bst);
if(ans==m)
printf("Yes\n");
else
printf("No\n");
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: