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

时间:2022-04-01 21:53:23

紫书上的原题 正好学数据结构拿出来做一下

不知道为什么bfs的队列一定要数组模拟……

还可以练习一下sscanf……

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
queue<int>ans;
struct Binary_Tree //二叉树
{
int value;
Binary_Tree *lchild,*rchild;
Binary_Tree()
{
value=;
lchild=rchild=NULL;
}
};
bool add(Binary_Tree *&Root,char *s,int value){ //向二叉树中加入点
if(Root==NULL)
Root=new Binary_Tree();
if(*s=='\0')
{
if(Root->value!=)
{
return false;
}
Root->value=value;
return true;
}
if(*s=='L') //寻找路径
{
return add(Root->lchild,s+,value);
}
else if(*s=='R')
{
return add(Root->rchild,s+,value);
}
return false;
}
void Delete(Binary_Tree *Root) //删除二叉树
{
if(Root==NULL) return ;
Delete(Root->lchild);
Delete(Root->rchild);
delete Root;
}
bool bfs(Binary_Tree *Root) //层次遍历
{
Binary_Tree *q[]; //数组模拟队列
int front=;
int rear=;
q[]=Root;
while (front<rear)
{
Binary_Tree *temp=q[front++];
if(!temp->value) //没有值就返回false
{
return false;
}
ans.push(temp->value); //当前节点入ans队
if(temp->lchild) //左孩子入队
{
q[rear++]=temp->lchild;
}
if(temp->rchild) //右孩子入队
{
q[rear++]=temp->rchild;
}
}
return true;
}
int main(){
Binary_Tree *Root=NULL;
char s[];
bool no=false;
while(true)
{
no=false;
Delete(Root);
Root=NULL; //二叉树初始化
while(true)
{
if(scanf("%s",s)==EOF) //读入
{
return ;
}
if(strcmp(s,"()")==)
{
break;
}
int val;
char loc[];
strcpy(loc,"");
sscanf(&s[],"%d",&val);
char *start=strchr(s,',')+;
sscanf(start,"%[A-Z]",&loc);
if(!add(Root,loc,val))
{
no=true;
}
}
if(no)
{
puts("not complete");
}
else
{
if(!bfs(Root))
puts("not complete");
else
{
while(!ans.empty())
{
int ll=ans.front();
ans.pop();
printf("%d%c",ll,ans.empty()?'\n':' ');
}
}
}
}
return ;
}
/* (11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) () */

[ An Ac a Day ^_^ ] hdu 1662 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. UVA 122 -- Trees on the level (二叉树 BFS)

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

  3. hdu 1622 Trees on the level

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

  4. UVA - 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  5. E - Trees on the level

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

  6. Trees on the level&lpar;指针法和非指针法构造二叉树)

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

  7. UVA&period;122 Trees on the level&lpar;二叉树 BFS&rpar;

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

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

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

  9. hdu 5200 Trees &lbrack; 排序 离线 2指针 &rsqb;

    传送门 Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 655 ...

随机推荐

  1. &lbrack;原创&rsqb;Centos7 从零编译配置Redis

    序言 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. Memca ...

  2. JAVA泛型详解2 转载

    转载地址:http://www.cnblogs.com/sunwei2012/archive/2010/10/08/1845938.html 普通泛型 class Point<T>{ // ...

  3. memory&lowbar;limit的一个bug &vert; 风雪之隅

    原文:memory_limit的一个bug | 风雪之隅 27 Nov 09 memory_limit的一个bug 作者: Laruence( ) 本文地址: http://www.laruence. ...

  4. SPSS二次开发

    在以前关于SPSS二次开发文章中留下过自己联系方式,差不多一年的时间,零零散散的和我取得联系的人也有几十位,看来对于SPSS二次开发的需求不少. Web SPSS系统是利用SPSS二次开发技术,使用户 ...

  5. ACM2114&lowbar;S&lbrack;I&rsqb;&lpar;1&Hat;3&plus;2&Hat;3&plus;3&Hat;3)

    #include<iostream> using namespace std; int main() { __int64 n,m,i,j,sum; while(cin>>n) ...

  6. hdu1003 最大连续子序和

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  7. bzoj 2588 树上主席树

    主席树上树,对于每个节点,继承其父亲的,最后跑f[x]+f[y]-f[lca]-f[fa[lca]] 去重竟然要减一,我竟然不知道?? #include<cstdio> #include& ...

  8. ROS机器人编程实践----琐碎知识点

    amcl原理: amcl将激光传感器数据与从地图预估的传感器数据相比较,给出可能的位姿.如果传感器数据和某个候选位姿处的预测数据相同,amcl就会给这个位姿一个较高的概率,反之,就会降低这个概率.概率 ...

  9. 机器学习技法笔记:05 Kernel Logistic Regression

    Roadmap Soft-Margin SVM as Regularized Model SVM versus Logistic Regression SVM for Soft Binary Clas ...

  10. css3整理--background-image

    background-image语法: background-image: url1,url2,...,urlN; 通过“,”分隔N张背景图片,background的所有其它属性需要配合该属性进行设置 ...