【题意】

给定一棵树每个结点的权重和路径(路径用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的更多相关文章

  1. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  2. hdu 1622 Trees on the level

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...

  3. [ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树

    紫书上的原题 正好学数据结构拿出来做一下 不知道为什么bfs的队列一定要数组模拟…… 还可以练习一下sscanf…… #include<stdio.h> #include<iostr ...

  4. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

  6. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

  7. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  8. E - Trees on the level

     Trees on the level  Background Trees are fundamental in many branches of computer science. Current ...

  9. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

随机推荐

  1. No input file specified的解决方法

    (一)IIS Noinput file specified 方法一:改PHP.ini中的doc_root行,打开ini文件注释掉此行,然后重启IIS 方法二:请修改php.ini找到; cgi.for ...

  2. windows Git的安装和使用

    一.本人是根据廖雪峰大神的个人官网学习的git,并有感而发以做笔记的方式写下这篇博客,希望可以帮助到自己和其他人,廖雪峰个人官网http://www.liaoxuefeng.com/ 二.声明我的电脑 ...

  3. LR中订单流程脚本2

    Action(){ //1.设置服务器的IP地址 //lr_save_string("192.168.1.12:8080", "ip"); lr_save_st ...

  4. COGS 2688. 鱼的感恩

    ★   输入文件:fool.in   输出文件:fool.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 从前有一个渔夫抓到了一条特别的鱼,放走了. 渔夫再次抓到了这条 ...

  5. SQLite C/C++ 教程

    目录 1安装 2 C/C++ Interface APIs 3连接到数据库 4创建表 5插入操作 6更新操作 7删除操作 安装 在我们开始使用SQLite在C / C++程序,我们需要确保SQLite ...

  6. 兼容IE6\7\8浏览器的html5标签的几个方案

    html5大行其道的时代已经到来.如果你还在等待浏览器兼容,说明你已经与web脱节几条街了.当然,这得益于移动客户端的蓬勃发展.如果还在纠结于,是否应该掌握html5和css3技术时,请狠狠的抽自己几 ...

  7. 1991: C语言实验——大小写转换

    1991: C语言实验——大小写转换 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 183  Solved: 109[Submit][Status][We ...

  8. 真爱 vs. 种姓:新一代印度人的婚恋观

    今日导读 “自由恋爱”是所有世界上所有有情人共同的心愿,而在印度,因为其根深蒂固的种姓制度,仍然有大批情侣只能听从父母的“包办婚姻”,被迫与心爱的人分离.但是最新的一项调查表明,印度的年轻一代开始出现 ...

  9. 引入了junit为什么还是用不了@Test注解

    pom文件明明引入了unit,为什么还是用不了@Test? 配置如下: <dependency> <groupId>junit</groupId> <arti ...

  10. C#导入有道词典单词本到扇贝

    由于扇贝查词没有有道方便,所以很多时候添加生词都是在使用有道词典,然后顺手就保存到了有道单词本,不过在扇贝记单词可以打卡,记单词更方便,进入扇贝页面后发现扇贝单词批量导入居然一次只支持10个,查了扇贝 ...