Trees on the level 树的层次遍历 UVA 122(BFS)
来源:互联网 发布:客户特殊要求矩阵图 编辑:程序博客网 时间:2024/06/13 22:20
- #include<cstdio>
- #include<vector>
- #include<queue>
- #include<cstring>
- using namespace std;
- const int maxn=260;
- struct node{
- bool have_value; //判断是否被付了值
- int value; //结点值
- node*left,*right; //左右子结点
- node():have_value(false),left(NULL),right(NULL){} //构造函数
- };
- char s[maxn]; //输入字符串
- int flag=0; //判断是否多次赋值,两次以上值为1
- vector<int> vec1; //用于储存将输出的值
- node*root; //根结点
- node* newnode(){ //此函数通过new 运算符申请内存生成新的node结构体并以指针形式返回
- return new node();
- }
- void addnode(int value,char* s){ //构建树,添加结点
- int len=strlen(s);
- node*u= root;
- for(int i=0;i<len;i++)
- if(s[i]=='L'){
- if(u->left == NULL )u->left=newnode(); //结点不存在,建立新结点
- u=u->left; //往左走
- }
- else if(s[i]=='R'){
- if(u->right == NULL)u->right=newnode();
- u=u->right;
- }
- if(u->have_value)flag=1; //已经被付过值,表明输入有误
- u->value=value;
- u->have_value=true; //标记此结点已经被负过值
- }
- bool read(){
- flag=0;
- root = newnode();
- while(true){
- if(scanf("%s",s)!=1)return false; //如果读到文件结束符,返回假
- if(!strcmp(s,"()"))break;
- int value;
- sscanf(&s[1],"%d",&value); //此函数从字符串中读取指定值,用于类似于scanf();
- addnode(value,strchr(s,',')+1); //strchr位于<cstring>头文件下,返回指定字符的地址
- }
- return true;
- }
- bool bfs(vector<int> & vec){
- queue<node *>q;
- vec.clear();
- q.push(root); //初始时只有一个根结点
- while(!q.empty()){
- node *u= q.front();q.pop();
- if(!u->have_value)return false; //如果为假,表明有结点未被赋值,输入有错
- vec.push_back(u->value); //将结点值放入到vector容器中,等待输出
- if(u->left!=NULL)q.push(u->left); //如果左右子结点存在,则放入到队列中
- if(u->right !=NULL)q.push(u->right);
- }
- return true;
- }
- int main(){
- while(read()){
- bool flag1=bfs(vec1); //flag1用于判断根结点是否存在值,不存在为假
- if(flag || !flag1)printf("not complete");
- else {
- printf("%d",vec1[0]);
- for(int i=1;i<vec1.size();i++)
- printf(" %d",vec1[i]);
- }
- printf("\n");
- }
- return 0;
- }
0 0
- uva 122 Trees on the level 二叉树的层次遍历(bfs)
- Trees on the level 树的层次遍历 UVA 122(BFS)
- UVA 122 Trees on the level (二叉树层次遍历)
- 例题6-7 UVA 122 - Trees on the level 树的层次遍历
- 例题6-7 二叉树的层次遍历 UVa 122 Trees on the level
- 例题6-7 树的层次遍历(Trees on the level, Duke 1993, UVa 122)
- UVa 122 Trees on the level(建树,层次遍历)
- UVa 122 Trees on the level(二叉树层次遍历)
- UVA 122 Trees on the level 二叉树层次遍历 数组&指针
- UVA 122 Trees on the level(指针二叉树,层次遍历)
- UVa 122 Trees on the level 建立二叉树BFS层序遍历
- UVA 122 && HDU 1622 Trees on the level(构造二叉树+BFS层序遍历)
- [UVA 122] Trees on the level 二叉树好难+BFS
- 【日常学习】【指针二叉树+BFS】Uva - 122 Trees on the level题解
- UVa 122 - Trees on the level【二叉树初接触(BFS)】
- UVa Problem 122 - Trees on the level
- UVA 122 - Trees on the level
- uva 122 - Trees on the level
- 自定义View想使用Scroller实现滑动效果,但是事件接收出现问题,只收到ACTION_DOWN,而无法收到ACTION_MOVE, ACTION_UP等消息的处理
- Qt的画图相关
- 三极管相关知识
- 【linux】task_struct结构体
- codeforces 754 A Lesha and array splitting
- Trees on the level 树的层次遍历 UVA 122(BFS)
- Python解析mnist出错的解决方法
- 支持向量机—SMO论文详解(序列最小最优化算法)
- Apache Shiro系列三:10分钟入门
- Qt画线条
- 操作系统概念-内核同步-信号量
- Ajax 理解,使用,记录
- sk_buff 详解(一)
- 利用QT中Qpainter画点,直线,弧线等简单图形