HDU 1622
http://acm.hdu.edu.cn/showproblem.php?pid=1622
白书上6.3.2二叉树层次遍历的例题,层次遍历用bfs,建立二叉树,很基础的题目
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue> using namespace std; struct node{
int vis,v;
node *l,*r;
}; node *root; node* newnode(){
node *u=new node;
if(u!=NULL){
u->vis=;
u->l=u->r=NULL;
}
return u;
} int failed; void addnode(int v,char* s){
int len=strlen(s);
node* u=root;
for(int i=;i<len;i++){
if(s[i]=='L'){
if(u->l==NULL)u->l=newnode();
u=u->l;
}
else if(s[i]=='R'){
if(u->r==NULL)u->r=newnode();
u=u->r;
}
}
if(u->vis)failed=;
u->v=v;
u->vis=;
} char s[]; void remove_tree(node* u){
if(u==NULL)return;
remove_tree(u->l);
remove_tree(u->r);
delete u;
} int input(){
failed=;
remove_tree(root);
root=newnode();
while(){
if(scanf("%s",s)==EOF)return ;
if(!strcmp(s,"()"))break;
int v;
sscanf(&s[],"%d",&v);
addnode(v,strchr(s,',')+);
}
return ;
} int st,ans[]; int bfs(){
queue <node*> q;
q.push(root);
while(!q.empty()){
node* u=q.front();
q.pop();
if(u->vis==)return ;
ans[st++]=u->v;
if(u->l!=NULL)q.push(u->l);
if(u->r!=NULL)q.push(u->r);
}
return ;
} int main(){
while(input()){
if(failed)puts("not complete");
else{
st=;
if(!bfs())puts("not complete");
else{
for(int i=;i<st;i++){
if(!i)
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
putchar('\n');
}
}
}
return ;
}
HDU 1622的更多相关文章
- hdu 1622 Trees on the level
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...
- 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
[题意] 给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历 [思路] 注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置 [Accep ...
- HDU - 1622 用到了层次遍历
题意: 给出一些字符串,由(,)包着,表示一个节点,逗号前面是节点的值,后面是节点从根节点走向自己位置的路线,输入以( )结尾,如果这组数据可以构成一个完整的树,输出层次遍历结果,否则输出not co ...
- hdu 1704 (Floyd 传递闭包)
Rank Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- oracle第一章
1.oracle对比sqlserver oracle sqlserver 数据文件.dbf 数据文件.mdf 控制文件.ctl 日志文件.log 日志文件.log 2.内置用户 1.sys ...
- MVC中使用HTML Helper类扩展HTML控件
文章摘自:http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/1415005.html MVC在view页面,经常需要用到很多封装好的HTML控件 ...
- 关于C#迭代器
>1 IEnumerator与IEnumerable IEnumerator与IEnumerable两个接口是用于实现迭代器的接品只要实现了IEnumerable就可以用foreach,linq ...
- ecshop数据库取数据
取出所有数据: test_getAll(); function test_getAll() { global $db; $sql = "SELECT user_id, user_name, ...
- "LC.exe" exited with code -1 错误
当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可
- 在Ios里UIWebView参入js
//修改图片大小适应webView宽高度 [webView stringByEvaluatingJavaScriptFromString: @"var sc ...
- myeclipse9.x,10.x 安装时出现pulse-one-64,failed to load the JNI shared library
- Oracle、Mysql、Sql Server语句的区别
1.空值的处理——判断是否为空,为空时取一个值,不为空时取另一个值 1).Sql Server 中 ISNULL(check_expression,replacement_value) 解释:如果ch ...
- LICEcap
LICEcap是一款简洁易用的动画屏幕录制软件,它可将屏幕录像的内容直接保存为高质量(每帧颜色数量可超过256)GIF动态图片格式.并且支持特别标记鼠标操作动态效果.
- Problem K 栈
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...