问题 N: 二叉查找树(Ⅲ)-判同
来源:互联网 发布:网管软件破解 编辑:程序博客网 时间:2024/05/16 01:24
前言必读!http://blog.csdn.net/hnust_v/article/details/51747743
问题 N: 二叉查找树(Ⅲ)-判同
题目描述N个数可以有N!个排列,每个排列都可以生成一棵二叉排序树,如果有重复数可能更多。对于其中任意两个排列,它们生成的二叉查找树可能相同,也可能不同。现在需要你编程序要判断。输入 包含多组测试数据。每组测试数据分成两部分。第一部分是模板树,由两行组成,第一行是空BST待插入关键字的数目n(1<=n<=100)。第2行是n个空格分开的正整数,值不超过100。 第二部分的第一行是一个整数数T,1<=T<=20 ,表示有T棵树需要判断是否和模板树一样,随后一共有2T行,每两行代表一棵树,格式和模板树一样。输出 对于每一棵要匹配的树,如果和模板树一样输出YES,否则输出NO。样例输入93 2 1 5 4 6 9 8 7391 2 3 4 5 6 7 8 993 2 1 5 6 4 9 8 799 8 7 6 5 4 3 2 1样例输出NOYESNO提示
//相同条件 大概是先中序一致,或者中后序一致,以防万一我三序全判断了(怪我上课没听-_-!)#include <bits/stdc++.h>using namespace std;typedef struct LNode{ LNode *Lchild,*Rchild; int info,flor;}LNode,*Fin;LNode Q[10000];int ERRM[3][10000],ERRD[3][10000];int CountM,CountD;void Print(Fin P,int &Count1,int &Count2,int &Count3,int flag){ if(P==NULL) return; if(flag==1) ERRM[0][++Count1]=P->info; else ERRD[0][++Count1]=P->info; Print(P->Lchild,Count1,Count2,Count3,flag); if(flag==1) ERRM[1][++Count2]=P->info; else ERRD[1][++Count2]=P->info; Print(P->Rchild,Count1,Count2,Count3,flag); if(flag==1) ERRM[2][++Count3]=P->info; else ERRD[2][++Count3]=P->info;}void Add(int a,int FLOR,LNode *P,LNode *Q){ // if(P.Lchild) printf("P==%d (*P.Lchild).info==%d\n",P.info,(*P.Lchild).info); Q->flor=FLOR; if(P->info > a) { if(P->Lchild != NULL) Add(a,FLOR+1,P->Lchild,Q); else {P->Lchild = Q;return;} } else if(P->info < a) { if(P->Rchild != NULL) Add(a,FLOR+1,P->Rchild,Q); else {P->Rchild = Q;return;} } else if(P->info == a) return; return;}void Creat(int n,int &Count,int flag){ Fin Root=NULL; int a;LNode P;Count=0; scanf("%d",&a); P.info=a;P.flor=0;P.Lchild=P.Rchild=NULL;//Print(&P); Root = &P; for(int i=2,a;i<=n;i++) { scanf("%d",&a); Q[i].info=a;Q[i].Lchild=Q[i].Rchild=NULL; Add(a,1,&P,&Q[i]); } Count=0; int O=0,I=0; Print(Root,Count,O,I,flag);}bool Judge(int A,int B){ if(A!=B) return false; for(int i=1;i<=A;i++) { // printf("%d %d ",ERRM[0][i],ERRD[0][i]); // printf("%d %d ",ERRM[1][i],ERRD[1][i]); // printf("%d %d\n",ERRM[2][i],ERRD[2][i]); if(ERRM[0][i]!=ERRD[0][i]) return false; if(ERRM[1][i]!=ERRD[1][i]) return false; if(ERRM[2][i]!=ERRD[2][i]) return false; } return true;}int main(){ //freopen("F:\\test.txt","r",stdin); // freopen("F:\\tsst.txt","w",stdout); int n; while(cin>>n) { Creat(n,CountM,1); int m;cin>>m; for(int i=1;i<=m;i++) { scanf("%d",&n); Creat(n,CountD,2); printf("%s\n",Judge(CountM,CountD)?"YES":"NO"); } } return 0;}
0 0
- 问题 N: 二叉查找树(Ⅲ)-判同
- 查找二叉查找树第N大的数
- 判满二叉树(二叉树)
- 问题 B: 进击的二叉查找树
- 查找--二叉查找树
- 二叉查找树查找后继,前驱的问题
- 关于二叉查找树的平均查找时间的问题
- 二叉树系列---求包含n个节点的二叉查找树的种类数
- 二叉树、二叉查找树
- 二叉树 & 二叉查找树
- 【查找结构】二叉查找树
- 查找之二叉树查找
- 查找之二叉树查找
- 查找:二叉查找树总结
- n 个节点可以组成的二叉树个数问题
- 已知先序后序遍历找n种二叉树问题
- 层序遍历二叉树,以及拓展问题,同层同行输出
- 问题 L: 二叉查找树(Ⅰ)-创建和遍历
- 如果在页面滚动的时候导航条一直在屏幕的上面
- Android中的AES加密
- leetcode 332 Reconstruct Itinerary
- 关于Java中Daemon线程
- ReentrantReadWriteLock读写锁的使用
- 问题 N: 二叉查找树(Ⅲ)-判同
- python学习-day1
- 使用c语言实现一个简单的易语言
- mina高并发短连接导致java.io.IOException: Too many open files解决方案
- 火车进站[Codevs 3595]
- hdu 2973 威尔逊定理+素数筛法
- 命名空间和PDO包含的常规属性和函数方法
- LeetCode 054 Spiral Matrix
- android中的网络解析xml,json,html框架