题目1009:二叉搜索树
来源:互联网 发布:screw it,let's do it 编辑:程序博客网 时间:2024/06/06 00:15
- 题目描述:
- 判断两序列是否为同一二叉搜索树序列
- 输入:
- 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
- 输出:
如果序列相同则输出YES,否则输出NO
- 样例输入:
25674325432675763420
- 样例输出:
YES
NO
#include<stdio.h>
#include<string.h>
void createSearchTree(int a[],char s[]);
int main(){
int n;
while (scanf("%d", &n) != EOF && n!=0){
char a[10];
int aa[1025];
char t[10];
int tt[1025];
for (int i = 0; i < 1025; i++){
aa[i] = -1;
tt[i] = -1;
}
scanf("%s", a);
createSearchTree(aa, a);
for (int i = 0; i < n; i++){
for (int i = 0; i < 1025; i++){
// aa[i] = -1;
tt[i] = -1;
}
scanf("%s", t);
createSearchTree(tt, t);
int p;
for (p = 0; p < 1025; p++){
if (aa[p] == tt[p])
continue;
else
break;
}
if (p == 1025)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
// 树利用数组存储时,令根节点为1,左孩子为2i,右孩子为2i+1
void createSearchTree(int a[], char s[]){
int length = strlen(s);
for (int i = 0; i < length; i++){
int temp = s[i] - '0';
for (int j = 1; j < 1025; ){
if (a[j] == -1){
a[j] = temp;
break;
}
else{
if (a[j]>temp)
j = 2 * j;
else
j = 2 * j + 1;
}
}
}
}
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 九度题目1009:二叉搜索树
- 九度题目1009:二叉搜索树
- Jobdu 题目1009:二叉搜索树
- 队列queue的详细讲解
- Spring-aop关系图
- Cust案例
- CSS
- hibernate配置二级缓存基本步骤
- 题目1009:二叉搜索树
- 剑指offer面试题[23]-从上往下打印二叉树(按层序打印)
- 顺序表应用6:有序顺序表查询
- Jsp内置对象
- 两点
- 【转】SpringMVC Controller
- 校验码之奇偶校验码
- PHP 的 HMAC_SHA1算法 实现
- linux 配置ip重启网络失败 出现未知连接(来源网络只作为个人笔记)