[ 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 ...
随机推荐
- svg图片转换为WEB字体图标
今天我学会了使用字体制作网站 icomoon.io 制作web文本图标.跟我一起学习吧! (1)字体制作网站 icomoon.io 点击 icomoon APP ---> imp ...
- 相机标定 matlab opencv ROS三种方法标定步骤(3)
三 , ROS 环境下 如何进行相机标定 刚开始做到的时候遇到一些问题没有记录下来,现在回头写的时候都是没有错误的结果了,首先使用ROS标定相机, 要知道如何查看节点之间的流程图 rosrun r ...
- 在CentOS上安装第三方软件库EPEL
Extra Packages for Enterprise Linux (EPEL)[企业版 Linux 附加软件包(以下简称 EPEL)]是一个由特别兴趣小组创建.维护并管理的,针对 红帽企业版 L ...
- Jquery几秒自动跳转
$(document).ready(function() { function jump(count) { window.setTimeout(function(){ count--; if(coun ...
- js作用域详解
// 作用域:(1)域:空间.范围.区域…… (2) 作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 浏览器: “JS解析器” 1)“找一些东西” :var func ...
- 学习smail注入遇到的坑
1.将需要被反编译的apk包解开之后,找到MainActivity,然后在OnCreate中添加需要加入注入的smail代码: Java代码: /** * 获取Android id * * @para ...
- MVC 5 属性路由中添加自己的自定义约束
介绍约束 ASP.NET MVC和web api 同时支持简单和自定义约束,简单的约束看起来像: routes.MapRoute("blog", "{year}/{mon ...
- maven 项目 pom.xml文件中配置的jar包下载报错
[ERROR] [ERROR] Some problems were encountered while processing the POMs:[ERROR] 'dependencies.depen ...
- maven 阿里镜像
<mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>al ...
- js 处理数据里面的空格
str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...