swust oj 981
统计利用二叉树存储的森林中树的棵数
输入
输入为接受键盘输入的由大写英文字符和"#"字符构成的一个字符串(用于创建对应的二叉树)。
输出
输出该用例对应的二叉树表示的森林中树的棵数。
样例输入
A#B#CD###
ABC####
AB##C##
ABCD###EF##G##H##
A##B##
样例输出
3
1
2
2
1
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
typedef char Datetype;
using namespace std;
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 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);
} int main()
{
tree *L = NULL;
x=;
creattree(L);
find(L);
cout<<x;
destroytree(L);
return ;
}
swust oj 981的更多相关文章
- [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 ...
随机推荐
- SpringBoot系列: SpringBoot Web项目中使用Shiro 之二
==================================Shiro 的加深理解:==================================1. Shiro 和 Spring 系组 ...
- MyBatis # $区别
方式一: <select id="getUserById" resultType="User" parameterType=”int”> SELEC ...
- [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.2 Cauchy - Green 应变张量
1. 引理 (极分解): 设 $|{\bf F}|\neq 0$, 则存在正交阵 ${\bf R}$ 及对称正定阵 ${\bf U},{\bf V}$ 使得 $$\bex {\bf F}={\bf ...
- 第29月第21天 ios android curl
1.curl https://github.com/gcesarmza/curl-android-ios/ 2.json https://github.com/danielmapar/cpp-scra ...
- C#利用VUDP.cs开发网络通讯应用例程
VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS VUDP文档地址:https://github.com/velscode ...
- jetty启动设置端口
nohup java -jar start.jar jetty.port=10010 命令不能在后台运行,ctrl+c程序就自动停止了,可以在命令后面加个&符号,就可以了 nohup java ...
- 20175204 张湲祯 2018-2019-2《Java程序设计》第二周学习总结
20175204 张湲祯 2018-2019-2<Java程序设计>第二周学习总结 教材学习内容总结 -第二章基本数据类型与数组要点: -标识符与关键字:标识符的第一个字符不能是数字字符: ...
- 给Ubuntu系统清理垃圾
原文地址:https://blog.csdn.net/levon2018/article/details/81746613 1.清理下载的软件包 不过与你想象的可能有很大的不同,Ubuntu系统在运 ...
- img标签里的value获取
简单描述:在img标签里的value存放了需要用到的值,但是在js中获取的时候,我直接写的就是$("#imgStr").val(),结果发现是空,后来查了一下,才知道,img的va ...
- SpringBoot 请求参数后端校验
1.例如: package com.model.user; import com.model.PageEntity;import lombok.Getter;import lombok.Setter; ...