[ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树
紫书上的原题 正好学数据结构拿出来做一下
不知道为什么bfs的队列一定要数组模拟……
还可以练习一下sscanf……
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
queue<int>ans;
struct Binary_Tree //二叉树
{
int value;
Binary_Tree *lchild,*rchild;
Binary_Tree()
{
value=;
lchild=rchild=NULL;
}
};
bool add(Binary_Tree *&Root,char *s,int value){ //向二叉树中加入点
if(Root==NULL)
Root=new Binary_Tree();
if(*s=='\0')
{
if(Root->value!=)
{
return false;
}
Root->value=value;
return true;
}
if(*s=='L') //寻找路径
{
return add(Root->lchild,s+,value);
}
else if(*s=='R')
{
return add(Root->rchild,s+,value);
}
return false;
}
void Delete(Binary_Tree *Root) //删除二叉树
{
if(Root==NULL) return ;
Delete(Root->lchild);
Delete(Root->rchild);
delete Root;
}
bool bfs(Binary_Tree *Root) //层次遍历
{
Binary_Tree *q[]; //数组模拟队列
int front=;
int rear=;
q[]=Root;
while (front<rear)
{
Binary_Tree *temp=q[front++];
if(!temp->value) //没有值就返回false
{
return false;
}
ans.push(temp->value); //当前节点入ans队
if(temp->lchild) //左孩子入队
{
q[rear++]=temp->lchild;
}
if(temp->rchild) //右孩子入队
{
q[rear++]=temp->rchild;
}
}
return true;
}
int main(){
Binary_Tree *Root=NULL;
char s[];
bool no=false;
while(true)
{
no=false;
Delete(Root);
Root=NULL; //二叉树初始化
while(true)
{
if(scanf("%s",s)==EOF) //读入
{
return ;
}
if(strcmp(s,"()")==)
{
break;
}
int val;
char loc[];
strcpy(loc,"");
sscanf(&s[],"%d",&val);
char *start=strchr(s,',')+;
sscanf(start,"%[A-Z]",&loc);
if(!add(Root,loc,val))
{
no=true;
}
}
if(no)
{
puts("not complete");
}
else
{
if(!bfs(Root))
puts("not complete");
else
{
while(!ans.empty())
{
int ll=ans.front();
ans.pop();
printf("%d%c",ll,ans.empty()?'\n':' ');
}
}
}
}
return ;
}
/* (11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) () */
[ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树的更多相关文章
- hdu 1622 Trees on the level(二叉树的层次遍历)
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- hdu 1622 Trees on the level
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- E - Trees on the level
Trees on the level Background Trees are fundamental in many branches of computer science. Current ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- hdu 5200 Trees [ 排序 离线 2指针 ]
传送门 Trees Accepts: 156 Submissions: 533 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
随机推荐
- DateTime & UTC 相互转化
public long ToUnixTime(DateTime date) { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.U ...
- 面试题-Java Web-网络通信
1.HTTP响应的结构是怎么样的? HTTP响应由三个部分组成:状态码(Status Code):描述了响应的状态.可以用来检查是否成功的完成了请求.请求失败的情况下,状态码可用来找出失败的原因.如果 ...
- js框架封装,模拟jQuery封装
模拟jQuery框架,利用原生的js技术,封装一个js框架,以加深对jQuery的常用api的使用和面向对象原理的理解:一:结构部分首先利用闭包,构造一个自执行函数,然后利用选择器函数Sizzle,获 ...
- ES 6 : Math对象的扩展
ES6在Math对象上新增了17个与数学相关的方法.所有这些方法都是静态方法,只能在Math对象上调用. 1.Math.trunc() Math.trunc方法用于去除一个数的小数部分,返回整数部分. ...
- Python学习笔记——基础篇【第五周】——random & time & datetime模块
random模块 随机数 mport random print random.random() print random.randint(1,2) print random.randrange(1,1 ...
- tomcat设置http自动跳转为https访问
一.生成服务器端证书文件 可以使用Windows系统或者Linux系统 (1)Windows环境 条件:已经安装JDK 步骤: 1.在运行里输入cmd进入命令窗口 2.进入JDK安装目录 如D:/P ...
- Quartz+log4net实现控制台程序定时运行,并且记录日志
c# 控制台程序定时运行,并记录日志. 组件Quartz + log4net Quartz定时运行程序,log4net记录日志. 首先建立控制台程序"QuartzTest" 安装Q ...
- EBS FORM FOLDER 开发,单元格无法使用右键
问题描述: 在使用folder开发FORM后,单元格无法使用右键,正常应该可以右键进行隐藏.显示.复制等操作. 通过对比发现是因ITEM属性中 弹出式菜单未设置导致. 解决方法: 设置弹出式菜单
- /home 和 /root
/root Linux超级权限用户root的家目录./home 如果我们建立一个用户,用户名是"xx",那么在/home目录下就有一个对应的/home/xx路径,用来存放用 ...
- TheSixthWeekJavaText
加密文档 实验要求编写一个算法加密一串英文字串. 设计思想:对于一个字符串,我们可以用String.charAt()方法依次取出其中的字符元素,组成一个字符数组.由于字符可以转化为short类型变量进 ...