二叉排序树
来源:互联网 发布:excel列变成数字 java 编辑:程序博客网 时间:2024/05/03 22:05
Problem Description
二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。 今天我们要判断两序列是否为同一二叉排序树
Input
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉排序树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉排序树。(数据保证不会有空树)
Output
Example Input
21234567899876543214321567890
Example Output
NONO
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{ char data; struct node *lc,*rc;}bitree;int flag;bitree * create(bitree * t,char ch){ if(!t) { t=(bitree *)malloc(sizeof(bitree)); t->data=ch; t->lc=t->rc=NULL; return t; } if(ch<t->data) { t->lc=create(t->lc,ch); } else { t->rc=create(t->rc,ch); } return t;}bitree * build(char str[],int len){ int i; bitree * tree; tree=(bitree *)malloc(sizeof(bitree)); tree->data=str[0]; tree->lc=tree->rc=NULL; for(i=1;i<len;i++) { tree=create(tree,str[i]); } return tree;}void judge (bitree * t,bitree *tr){ if(t&&tr) { if(t->data!=tr->data) { flag=1; return ; } judge(t->lc,tr->lc); judge(t->rc,tr->rc); }}int main(){ int len,n; char str[11]; bitree * tree,*tree1; while(scanf("%d",&n),n) { scanf("%s",str); len=strlen(str); tree=build(str,len); while(n--) { flag=0; scanf("%s",str); len=strlen(str); tree1=build(str,len); judge(tree,tree1); if(flag) { printf("NO\n"); } else { printf("YES\n");
}
}
return 0;
}
0 0
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 互联网企业安全建设之路:互联网企业为什么要做安全?
- Java锁的种类以及辨析
- spring MVC中数据绑定之日期类型
- 前后端分离趋势谈
- _Net代码生成器
- 二叉排序树
- 随笔
- java,list分段(一)
- MySQL数据库的使用
- yii2高级模板使用一个域名管理前后台
- 简析TCP的三次握手与四次分手
- 如何使用mysql命令行
- Nginx 模块添加
- VS2010对C++11的支持程度