swust oj 987
输出用先序遍历创建的二叉树是否为完全二叉树的判定结果
输入
- 输入为接受键盘输入的由大写英文字符和"#"字符构成的一个字符串(用于创建对应的二叉树)。
输出
- 对应的二叉树是否为完全二叉树的判断结果。若是输出"Y",否则输出"N"。
样例输入
- A##
- ABC####
- AB##C##
- ABCD###EF##G###
- A##B##
- ABC##D##EG###
样例输出
- Y
- N
- Y
- N
- Y
- Y
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdlib>
- #include<cstdio>
- typedef char Datetype;
- using namespace std;
- Datetype value;
- int x;
- typedef struct link{
- Datetype date;
- struct link *lchild;
- struct link *rchild;
- }tree;
- typedef struct queue{
- tree *data;
- struct queue *next;
- }que;
- typedef struct {
- que *front;
- que *rear;
- }lin;
- void Initqueue(lin *&L)
- {
- L=(lin *)malloc(sizeof(lin));
- L->front=L->rear=NULL;
- }
- void destroyed(lin *&L)
- {
- que *p=NULL,*r=NULL;
- p=L->front;
- while(p!=NULL)
- {
- r=p;
- p=p->next;
- free(r);
- }
- free(L);
- }
- bool pop(lin *&L, tree *&e)
- {
- que *p;
- if(L->rear==NULL)
- return false;
- p=L->front;
- if(L->rear==L->front)
- L->front=L->rear=NULL;
- else
- L->front=p->next;
- e=p->data;
- free(p);
- return true;
- }
- int empty(lin *&L)
- {
- return (L->rear==NULL);
- }
- void push(lin *&L,tree *e)
- {
- que *p;
- p = (que *)malloc(sizeof(que));
- p->data=e;
- p->next=NULL;
- if(L->rear==NULL)
- {
- L->front=p;
- L->rear=p;
- }
- else
- {
- L->rear->next=p;
- L->rear=p;
- }
- }
- void creattree(tree *&L) //先序建立二叉树
- {
- char c;
- cin>>c;
- if(c=='#')
- L=NULL;
- else
- {
- L = (tree *)malloc(sizeof(tree)) ;
- L->date=c;
- creattree(L->lchild);
- creattree(L->rchild);
- }
- }
- void find(tree *L) //找树的棵树
- {
- if(L!=NULL)
- {
- x++;
- find(L->rchild);
- }
- }
- void destroytree(tree *&L) //销毁树
- {
- if(L!=NULL)
- {
- destroytree(L->lchild);
- destroytree(L->rchild);
- free(L);
- }
- }
- int deep(tree *L) //深度
- {
- int ldep,rdep,max;
- if(L!=NULL)
- {
- ldep=deep(L->lchild);
- rdep=deep(L->rchild);
- max=ldep>rdep?ldep+:rdep+;
- return max;
- }
- else
- return ;
- }
- void finddegree(tree *&L, int n) //找最大度数
- {
- if(L!=NULL)
- {
- if(x<n)
- x=n;
- finddegree(L->lchild,n);
- finddegree(L->rchild,n+);
- }
- }
- void run(tree *L) //层次遍历
- {
- tree *p=L;
- lin *qu;
- Initqueue(qu);
- if(L!=NULL)
- push(qu,p);
- while(!empty(qu))
- {
- pop(qu,p);
- cout<<p->date;
- if(p->lchild!=NULL)
- push(qu,p->lchild);
- if(p->rchild!=NULL)
- push(qu,p->rchild);
- }
- destroyed(qu);
- }
- void displayhou(tree *&L) //后序输出
- {
- if(L!=NULL)
- {
- displayhou(L->lchild);
- displayhou(L->rchild);
- cout<<L->date;
- }
- }
- void displaypre(tree *&L) //先序输出
- {
- if(L!=NULL)
- {
- cout<<L->date;
- displaypre(L->lchild);
- displaypre(L->rchild);
- }
- }
- void creatinpre(tree *&L ,char *in,char *pre,int x)//根据中先序确定后序
- {
- int k=;
- char *p;
- if(x<=)
- {
- L=NULL;
- return ;
- }
- L=(tree *)malloc(sizeof(tree));
- L->date = *pre;
- for(p=in ; p<in+x; p++)
- {
- if(*p==*pre)
- break;
- }
- k=p-in;
- creatinpre(L->lchild,in,pre+,k);
- creatinpre(L->rchild,p+,pre+k+,x-k-);
- }
- void creatinhou(tree *&L ,char *in,char *pre,int x) //根据中后序确定先序
- {
- int k=;
- char *p;
- if(x<=)
- {
- L=NULL;
- return ;
- }
- L=(tree *)malloc(sizeof(tree));
- L->date = *pre;
- for(p=in ; p>in-x; p--)
- {
- if(*p==*pre)
- break;
- }
- k=in-p;
- creatinhou(L->rchild,in,pre-,k);
- creatinhou(L->lchild,p-,pre-k-,x-k-);
- }
- void findson(tree *&L) //找指定节点的儿子
- {
- if(L!=NULL)
- {
- if(L->date==x)
- {
- if(L->lchild==NULL)
- cout<<"L:#";
- else
- cout<<"L:"<<L->lchild->date;
- if(L->rchild==NULL)
- cout<<",R:#";
- else
- cout<<",R:"<<L->rchild->date;
- return ;
- }
- findson(L->lchild);
- findson(L->rchild);
- }
- }
- void finddad(tree *&L) //找指定节点的父亲节点
- {
- if(L!=NULL)
- {
- if(L->lchild!=NULL&&L->lchild->date==x||L->rchild!=NULL&&L->rchild->date==x)
- {
- cout<<L->date;
- return ;
- }
- finddad(L->lchild);
- finddad(L->rchild);
- }
- }
- int find_the_one_degree(tree *&L) //找寻某指定节点的度
- {
- if(L!=NULL)
- {
- if(L->date==value)
- {
- if(L->lchild!=NULL&&L->rchild==NULL||L->lchild==NULL&&L->rchild!=NULL)
- return ;
- else if(L->lchild==NULL&&L->rchild==NULL)
- return ;
- else
- return ;
- }
- find_the_one_degree(L->lchild);
- find_the_one_degree(L->rchild);
- }
- }
- void exchange(tree *&L) //交换左右儿子的值
- {
- if(L!=NULL)
- {
- tree *p;
- p=L->lchild;
- L->lchild=L->rchild;
- L->rchild=p;
- exchange(L->lchild);
- exchange(L->rchild);
- }
- }
- void displayin(tree *&L) //中序输出
- {
- if(L!=NULL)
- {
- displayin(L->lchild);
- cout<<L->date;
- displayin(L->rchild);
- }
- }
- bool banlancetree(tree *&L) //平衡树
- {
- if(L==NULL)
- return true;
- int left=deep(L->lchild);
- int right=deep(L->rchild);
- int gas=left-right;
- if(gas>||gas<-)
- return false;
- return banlancetree(L->lchild)&&banlancetree(L->rchild);
- }
- bool perfecttree(tree *&L,int deepth) //完全二叉树的判定
- {
- if(L==NULL)
- return true;
- if(L->rchild!=NULL&&L->lchild==NULL)
- return false;
- if(x-deepth>&&L->rchild==NULL)
- return false;
- return perfecttree(L->lchild,deepth+)&&perfecttree(L->rchild,deepth+);
- }
- int main()
- {
- tree *L = NULL;
- creattree(L);
- x=deep(L);
- if(perfecttree(L,))
- cout<<"Y";
- else
- cout<<"N";
- destroytree(L);
- return ;
- }
swust oj 987的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
- [Swust OJ 385]--自动写诗
题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535 Descripti ...
随机推荐
- 记一次简单的PHP代码审计(SSRF案例)
题目链接: http://oj.momomoxiaoxi.com:9090/ 用dirsearch对网址进行扫描,发现robots.txt 命令行: python3 dirsearch.py -u & ...
- oracle查询某张表的外键,并用 truncate 命令有外键的表中的数据
注:本文来源于<oracle查询某张表的外键(最终解决办法)> 一:几个查询表外键的脚本 select b.table_name, b.column_name from user_cons ...
- 最优的路线(floyd最小环)
问题描述 学校里面有N个景点.两个景点之间可能直接有道路相连,用Dist[I,J]表示它的长度:否则它们之间没有直接的道路相连.这里所说的道路是没有规定方向的,也就是说,如果从I到J有直接的道路,那么 ...
- spring-cloud-config-server分布式配置中心
spring cloud config是一个基于http协议的远程配置实现方式.通过统一的配置管理服务器进行配置管理,客户端通过https协议主动的拉取服务的的配置信息,完成配置获取. spring ...
- docker启动,重启,停止容器
docker 启动已经停止的容器 docker start 容器ID或容器名 docker 停止容器 docker stop 容器ID或容器名 docker 启动一个容器 -d:后台运行 -p:端口映 ...
- 一个spring mvc 需要用到到文件
一. 类 org.springframework.stereotype.Controller; org.springframework.web.bind.annotation.RequestMappi ...
- 分享:使用 TypeScript 编写的游戏代码
<上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...
- 关于vue的computed、filters、watch
filters 这个属性大家可能用的不是很多 因为一般的数组过滤我们用 es6的filter就能完成了 我想到一个场景,网上买书促销 满100减50 满两百减100 <input type=&q ...
- CF809E Surprise me!
题解: 一道很套路的题目 首先一个结论 $\phi(xy)=\frac{\phi(x)*\phi(y)*gcd(x,y)}{\phi(gcd(x,y))}$ 这个按照$\phi$的定义很容易知道 然后 ...
- 《ServerSuperIO Designer IDE使用教程》-4.增加台达PLC驱动及使用教程,从0到1的改变。发布:v4.2.3版本
v4.2.3 更新内容:1.优化数据存储部分,提高效率.2.修复数据库服务停止造成程序异常退出的现象.3.修复本机没有串口造成无法增加设备驱动的情况.4.增加编辑设备和监测点配置信息功能.5.增加台达 ...