【二叉树】hdu 1622 Trees on the level
【题意】
给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历
【思路】
注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置
【Accepted】
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; int num;
const int maxn=;
char str[maxn];
struct node{
int num;
node *lef;
node *rig;
};
node *root;
bool tag;
void bfs(node *rt){
queue<node *> Q;
Q.push(rt);
while(!Q.empty()){
node *q=Q.front();
Q.pop();
if(q==rt){
printf("%d",q->num);
}else{
printf(" %d",q->num);
}
if(q->lef){
Q.push(q->lef);
}
if(q->rig){
Q.push(q->rig);
}
free(q);
}
}
bool isComplete(node *rt){
queue<node *> Q;
Q.push(rt);
while(!Q.empty()){
node *q=Q.front();
Q.pop();
if(q->num==-){
return false;
}
if(q->lef!=NULL){
Q.push(q->lef);
}
if(q->rig!=NULL){
Q.push(q->rig);
}
}
return true;
}
void print(node *root){
if(tag==false){
printf("not complete\n");
return;
}
bool flag=isComplete(root);
if(!flag){
printf("not complete\n");
return;
}
bfs(root);
puts("");
}
int main(){
root=(node *)malloc(sizeof(node));
root->num=-;
root->lef=NULL;
root->rig=NULL;
tag=true;
while(scanf("%s",str)!=EOF){
if(!strcmp(str,"()")){
print(root);
root=(node *)malloc(sizeof(node));
root->num=-;
root->lef=NULL;
root->rig=NULL;
tag=true;
}else{
sscanf(&str[],"%d",&num);
// printf("%d\n",num);
char *comma=strchr(str,',');
char *path=comma+;
node *now=root;
for(char *i=path;*i!=')';i++){
if(*i=='L'){
if(now->lef==NULL){
node *nd=(node *)malloc(sizeof(node));
nd->num=-;
nd->lef=NULL;
nd->rig=NULL;
now->lef=nd;
}
now=now->lef;
}else{
if(now->rig==NULL){
node *nd=(node *)malloc(sizeof(node));
nd->num=-;
nd->lef=NULL;
nd->rig=NULL;
now->rig=nd;
}
now=now->rig;
}
}
if(now->num!=-){
tag=false;
}else{
now->num=num;
}
}
}
free(root);
return ;
}
【二叉树】hdu 1622 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 ( ...
- hdu 1622 Trees on the level
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...
- [ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树
紫书上的原题 正好学数据结构拿出来做一下 不知道为什么bfs的队列一定要数组模拟…… 还可以练习一下sscanf…… #include<stdio.h> #include<iostr ...
- 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- ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- E - Trees on the level
Trees on the level Background Trees are fundamental in many branches of computer science. Current ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
随机推荐
- MyBatis插入数据之后返回插入记录的id
MyBatis插入数据的时候,返回该记录的id<insert id="insert" keyProperty="id" useGeneratedKeys= ...
- EJB2.0教程 详解EJB技术及实现原理
EJB是什么呢?EJB是一个J2EE体系中的组件.再简单的说它是一个能够远程调用的javaBean.它同普通的javaBean有两点不同.第一点,就是远程调用.第二点,就是事务的功能,我们在EJB中声 ...
- LINUX提高openfire并发数(网上收集)
谷歌博客地址:http://tsaiquinn.blogspot.com/2014/10/linuxopenfire.html 影响连接数的元素包含三种:1)Linux的系统参数2)进程自身可以创建的 ...
- codevs 2776 寻找代表元
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 广州二中苏元实验学校一共有n个社团,分别用1到n编号.广州二中苏元实验 ...
- 虚拟机ubuntu16.0 安装 mysql 主机配置访问
在bantu服务器中安装如下命令 sudo apt-get install mysql-server sudo apt-get install mysql-client安装成功之后 进入配置文件 ...
- ios设备屏幕尺寸与分辨率
iOS 设备的屏幕尺寸.分辨率及其屏幕边长比例详细情况是怎样的? 根据屏幕尺寸和分辨率,ios现在数起来有6个版本.一,3GS:二,4s为代表:三,iphone5:四,ipad2为代表:五,ipad4 ...
- BZOJ4513 SDOI2016 储能表 记忆化搜索(动态规划)
题意: 题面中文,不予翻译:SDOI2016储能表 分析: 据说有大爷用一些奇怪的方法切掉了这道题%%%%% 这里用的是大众方法——动态规划. 其实这是一道类似于二进制数位dp的动态规划题,(但是实际 ...
- python mysql备份脚本
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: pyinnobackup.py #@time: 2018/12/11 1 ...
- mysql:10道mysql查询语句面试题
表结构 学生表student(id,name) 课程表course(id,name) 学生课程表student_course(sid,cid,score) 创建表的sql代码 ```sql creat ...
- 使用dmidecode在Linux下获取硬件信息
dmidecode命令可以让你在Linux系统下获取有关硬件方面的信息.dmidecode的作用是将DMI数据库中的信息解码,以可读的文本方式显示.由于DMI信息可以人为修改,因此里面的信息不一定是系 ...