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 ...
随机推荐
- while、do while练习——7月24日
while循环的格式是for循环的变形 //while 循环(当循环),是for循环的变形 //for(int i=0;i<=5;i++) //{ // Console.WriteLine(&q ...
- F-Dining Cows(POJ 3671)
Dining Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7584 Accepted: 3201 Descr ...
- 交互设计师常用的web设计模式(转)
交互设计师在设计线框图原型时,熟知常见的web设计模式很有帮助,做到“心中有数”才能创造出符合需求,用户易学易用的界面来.所谓“没有必要重复发明轮子”,模式往往容易解决常见问题,正确的模式能帮用户熟悉 ...
- 静态工具类中使用注解注入service
转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是 ...
- ODBC 小例
#include "stdafx.h"#include <windows.h>#include <stdio.h>#include <iostream ...
- Java:JDK安装
访问Oracle网站www.oracle.com/technetwork/java/javase/downloads下载jdk 安装JDK时,不建议安装在有空格的路径名下,例如该目录c:\Progra ...
- 使用ContentObserve监听用户发出的短信
import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.app.Activit ...
- eclipse关联tomcat并且部署java web应用程序
http://www.ibm.com/developerworks/cn/opensource/os-eclipse-tomcat/
- 大过年的,不下班的,上个Android文件操作类(内部存储和sd卡均可)
package com.kkdiangame.UI.res; import java.io.ByteArrayOutputStream; import java.io.File; import jav ...
- iOS解决两个静态库的冲突 duplicate symbol
http://blog.163.com/023_dns/blog/static/118727366201391544630380/ 场景: 解决TencentOpenAPI.framework与Zba ...