输出利用二叉树存储的普通树的度

1000(ms)
10000(kb)
2619 / 5991
普通树可转换成相应的二叉树(该二叉树的根结点一定缺少右儿子),反之亦然。故而可以根据相应的转换方法去统计某一二叉树对应的普通树的度。普通树的度为其结点儿子数的最大值。相应的二叉树可利用二叉树的先序递归遍历算法创建。先序递归遍历建立二叉树的方法为:按照先序递归遍历的思想将对二叉树结点的抽象访问具体化为根据接收的数据决定是否产生该结点从而实现创建该二叉树的二叉链表存储结构。约定二叉树结点数据为单个大写英文字符。当接收的数据是字符"#"时表示该结点不需要创建,否则创建该结点。最后再统计该二叉树对应的森林中树的棵数。需要注意输入数据序列中的"#"字符的序列及个数关系,这会最终决定创建的二叉树的形态(序列里面允许无效字符但需要正确处理)。

输入

输入为接受键盘输入的由大写英文字符和"#"字符构成的一个字符串(用于创建对应的二叉树)。

输出

若表示的二叉树对应普通树,则该普通树的度;否则输出ERROR。

样例输入

AB#CD##E###
ABC####
AB##C##
ABCD###EF##G###
A##B##

样例输出

3
1
ERROR
3
 #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 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);
} int main()
{
tree *L = NULL;
x=;
creattree(L);
if(L->rchild!=NULL)
cout<<"ERROR";
else
{
finddegree(L,);
cout<<x;
}
destroytree(L);
return ;
}

swust oj 982的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  5. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. [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 ...

  9. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

随机推荐

  1. [Android] Android 类似今日头条顶部的TabLayout 滑动标签栏 效果

    APP市场中大多数新闻App都有导航菜单,导航菜单是一组标签的集合,在新闻客户端中,每个标签标示一个新闻类别,对应下面ViewPager控件的一个分页面,今日头条, 网易新闻等. 本文主要讲的是用:T ...

  2. SpringBoot系列: url重定向和转发

    Web UI项目中, 很多 Spring controller 视图函数直接返回 html 页面, 还有一些视图函数是要重定向或转发到其他的 url 上. redirect 和 forward的区别: ...

  3. JQuery对象关系图

    上图转自:http://www.cnblogs.com/haogj/archive/2010/04/19/1715762.html 自定义函数示例: $.fn.jAccordionunfold = f ...

  4. 2017-2018-2 PDE 讨论班

    等等. 第一次上课居然忘记怎么让笔记本电脑和投影仪相连了. 有两个接口. 一个在外面, 没用. 一个盖着了, 忘记翻开了.

  5. 第29月第18天 mac evpp环境

    1.boost https://github.com/Orphis/boost-cmake/ 2.evpp brew install libevent brew install glog /usr/l ...

  6. Tupper自我指涉公式生成器

  7. 2.解决虚拟机中centos联网的问题

    首先:打开虚拟机的编辑菜单,选择“虚拟机网络编辑器” 虚拟机网络编辑器 在虚拟机网络编辑器中选择还原默认设置 接下来开启CentOS7虚拟机 在这里需要注意的是必需以管理员身份来进行设置,所以要用管理 ...

  8. POJ 3304 Segments(直线)

    题目: Description Given n segments in the two dimensional space, write a program, which determines if ...

  9. orm总结

    x先谈谈java方面的. mybatis优点是基本啥都有了,sql统一管理,只需接口就可以了,缺点是对于复杂语句的执行还是麻烦,一般还是在程序里解决,自带的动态sql功能较弱不说,重点是调式时还是麻烦 ...

  10. IT行业中文资源网址集绵

    1. IT网址:https://github.com/ityouknow/awesome-list 2.后端架构师网址:https://github.com/xingshaocheng/archite ...