SDUT-二叉排序树
来源:互联网 发布:php网站文件管理 编辑:程序博客网 时间:2024/05/22 00:20
二叉排序树
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss
Problem Description
二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。 今天我们要判断两序列是否为同一二叉排序树
Input
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉排序树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉排序树。(数据保证不会有空树)
Output
Example Input
21234567899876543214321567890
Example Output
NONO
Hint
Author
#include <bits/stdc++.h>using namespace std;char s[111],ss[111];int flag;//设置一个变量计算两棵树有几个节点节点值相同,若等于总共的节点个数则可构成同一二叉排序树typedef struct node{ char data; struct node*l,*r;}Bintree;Bintree*creat(Bintree*root,char x){ if(root!=NULL) { if(x<root->data)root->l=creat(root->l,x); else root->r=creat(root->r,x); } else { root=(Bintree*)malloc(sizeof(Bintree)); root->data=x; root->l=root->r=0; } return root;}void judge(Bintree*root,Bintree*croot){ if(root==NULL||croot==NULL) { return; } if(root&&croot) { if(root->data!=croot->data) return ; else { flag++; judge(root->l,croot->l); judge(root->r,croot->r); } }}int main(){ int n,i,len; while(scanf("%d",&n),n) { Bintree*root=NULL; scanf("%s",s); len=strlen(s); for(i=0; i<len; i++) { root=creat(root,s[i]); } while(n--) { Bintree*croot=NULL; flag=0; scanf("%s",ss); for(i=0; i<len; i++) { croot=creat(croot,ss[i]); } judge(root,croot); if(flag!=len) printf("NO\n"); else printf("YES\n"); } } return 0;}
阅读全文
0 0
- sdut 二叉排序树
- SDUT-二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- 二叉排序树 (sdut oj 2482)
- sdut oj2482 二叉排序树
- SDUT oj 二叉排序树
- 二叉排序树——SDUT
- <sdut-ACM> 2482二叉排序树
- SDUT-数据结构实验之查找一:二叉排序树
- SDUT 3373 数据结构实验之查找一:二叉排序树
- SDUT 3373 数据结构实验之查找一:二叉排序树
- 数据结构实验之查找一:二叉排序树 (sdut oj 3373)
- sdut oj3373 数据结构实验之查找一:二叉排序树
- 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程
- iOS高级调试&逆向技术-汇编寄存器调用
- 算法 c语言 快速排序
- HQL常用的查询语句
- 指定月份获取每周开始和截止时间,不足一周下个月的补足一周,以周六为标准
- SDUT-二叉排序树
- Java管理扩展指南之JMX技术总览
- 机房重构未能加载文件或程序集“DAL”或它的某一个依赖项。系统找不到指定的文件。
- c++ strstream
- MySQL_5.7.17的安装与配置
- c指针
- Handler机制和Can't create handler inside thread that has not called Looper.prepare()异常
- async 和await的结合promise用法
- Linux系统运维工程该具备哪些素质