九度 oj 1009
来源:互联网 发布:linux服务器编程 编辑:程序博客网 时间:2024/05/05 10:48
- 题目描述:
- 判断两序列是否为同一二叉搜索树序列
- 输入:
- 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
- 输出:
如果序列相同则输出YES,否则输出NO
- 样例输入:
25674325432675763420
- 样例输出:
YESNO
- 来源:
2010年浙江大学计算机及软件工程研究生机试真题
#include<iostream>#include<stdio.h>#include<string.h>using namespace std; struct node{ node *lchild; node *rchild; int c; }tree[110]; int loc; node *creat(){ tree[loc].lchild=tree[loc].rchild=NULL; return &tree[loc++]; } char str1[25],str2[25]; int size1,size2; char *str; int *size; void postorder(node *t) { str[(*size)++]=t->c+'0'; if(t->lchild!=NULL) { postorder(t->lchild); } if(t->rchild!=NULL) { postorder(t->rchild); } } void inorder(node *t) { if(t->lchild!=NULL) { inorder(t->lchild); } str[(*size)++]=t->c+'0'; if(t->rchild!=NULL) { inorder(t->rchild); } } node *insert(node *t,int x) { if(t==NULL) { t=creat(); t->c=x; return t; } else if(x<t->c) { t->lchild=insert(t->lchild,x); } else if(x>t->c) { t->rchild=insert(t->rchild,x); } return t; } int main() { int n; char tmp[12]; while(scanf("%d",&n)!=EOF&&n!=0) { loc=0; node *t=NULL; scanf("%s",tmp); for(int i=0;tmp[i]!=0;i++) { t=insert(t,tmp[i]-'0'); } size1=0; str=str1; size=&size1; postorder(t); inorder(t); str1[size1]=0; while(n--!=0) { scanf("%s",tmp); node *t2=NULL; for(int i=0;tmp[i]!=0;i++) { t2=insert(t2,tmp[i]-'0'); } size2=0; str=str2; size=&size2; postorder(t2); inorder(t2); str2[size2]=0; puts(strcmp(str1,str2)==0 ? "YES" : "NO"); } } }
0 0
- 九度OJ 1009
- 九度 oj 1009
- 【九度OJ】:九度OJ 1050
- 【九度OJ】:九度OJ 1053
- 【九度OJ】:九度OJ 1056
- 【九度OJ】:九度OJ 1059
- 【九度OJ】:九度OJ 1206
- 九度OJ 1009 二叉搜索树
- 九度oj-1009-二叉搜索树
- 九度oj 二叉排序树 1201,1009 c++
- 九度OJ - 1009 - 二叉搜索树
- 九度OJ 1009 二叉搜索树
- 九度OJ-1009-二叉搜索树
- 九度OJ 1009 二叉搜索树
- 九度OJ:1000
- 【九度OJ】 1006
- 九度 OJ 1499
- 九度OJ-1002
- PHP使用pdo连接数据库
- 1059. Prime Factors (25)
- 如何在CentOS上查看Windows的ntfs分区
- POJ 1979 Red and Black (DFS)
- 弹框 IOS 7 IOS 8
- 九度 oj 1009
- cookie与session的区别
- ECSHOP程序文件解析-cls_mysql文件注释说明
- ListenableFuture in Guava
- HDU 3328 Flipper 魔术纸牌
- 最小二乘回归树生成算法
- 有关c语言的++与--
- ecshop 资料整理
- GTK入门学习:布局容器之表格布局