【二叉树】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 ...
随机推荐
- SQLServer同一实例下事务操作
参考代码: 引用Dapper public bool OrderAdd2(User user, Order order) { string dbString = ConfigurationManage ...
- [文章泛读] The varying faces of a program transformation systems (ACM Inroads, 2012)
Beevi S. Nadera, D. Chitraprasad, and Vinod S. S. Chandra. 2012. The varying faces of a program tran ...
- jquery的load方法
load方法指定一个界面会显示在目标的标签内部 比如MVC的一个分部视图页面想要显示在某个标签里面,可以写成 $(标签ID).load(分部视图名称,data) 其中第二个参数可选,主要是一些需要传递 ...
- java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal问题解决
使用Maven构建项目并加载spring配置文件时,报如下异常 Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTrav ...
- atom 自定义快捷键
'atom-text-editor': 'shift-alt-i':'core:move-up' 'shift-alt-space':'core:move-down' 'shift-alt-l':'c ...
- 设置section的距离
在ios7中使用group类型的tableview时,第一个section距离navigationbar的距离很大,不符合这边的设计图.使用 myTableView . sectionHeaderHe ...
- SVN的使用二
一,打开SCM 在xcode中,点击菜单: File -> Source Control –> Repositories 二,连接SVN服务器 1, 2,配置SVN服务器地址(http:/ ...
- js事件(事件冒泡与事件捕获)
事件冒泡和事件捕获分别由微软和网景公司提出,这两个概念都是为了解决页面中事件流(事件发生顺序)的问题. <div id='aa' click='po'> <p id='bb' cli ...
- MYSQL中批量替换某个字段的部分数据
1.修改字段里的所有含有指定字符串的文字 UPDATE 表A SET 字段B = replace(字段B, 'aaa', 'bbb') example: update table set url= ...
- 又面试了Python爬虫工程师,碰到这么几道面试题,Python面试题No9
第1题:动态加载又对及时性要求很高怎么处理? 如何知道一个网站是动态加载的数据? 用火狐或者谷歌浏览器 打开你网页,右键查看页面源代码,ctrl +F 查询输入内容,源代码里面并没有这个值,说明是动态 ...