uva-122 树的层次遍历
来源:互联网 发布:分布式通信 java 编辑:程序博客网 时间:2024/05/21 11:12
uva 122
白书代码
#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;const int maxn=300;char s[maxn];bool failed;struct node{ bool have_value; int v; node *left,*right; node():have_value(false),left(NULL),right(NULL){}};node* root;node* newnode(){return new node();}void addnode(int v,char* s){ int n=strlen(s); node* u=root; for (int i = 0; i < n; 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) failed=true; u->v=v; u->have_value=true;}bool bfs(vector<int>& ans){ queue<node*>q; ans.clear(); q.push(root); while(!q.empty()){ node* u=q.front();q.pop(); if(!u->have_value)return false; ans.push_back(u->v); if(u->left!=NULL)q.push(u->left); if(u->right!=NULL)q.push(u->right); } return true;}bool read_input(){ failed=false; root=newnode(); for(;;){ if(scanf("%s",s)!=1)return false; if(!strcmp(s,"()")) break; int v; sscanf(&s[1],"%d",&v); addnode(v,strchr(s,',')+1); } return true;}int main() { vector<int> v; while(read_input()){ bool flag=bfs(v); int size=v.size(); if(!flag||failed)printf("not complete\n"); else for(int i=0;i<size;i++)printf(i==size-1?"%d\n":"%d ",v[i]); } return 0;}
阅读全文
0 0
- uva-122 树的层次遍历
- UVA 122 树的层次遍历
- UVA 122 树的层次遍历
- uva 122 树的层次遍历
- UVA 122 树的层次遍历
- uva-122 树的层次遍历
- 例题 6-7 树的层次遍历 UVa 122 建树+宽度优先遍历
- Uva 11234 Expressions (二叉树的层次遍历)
- UVA 122 Trees on the level (二叉树层次遍历)
- 例题6-7 UVA 122 - Trees on the level 树的层次遍历
- uva 122 Trees on the level 二叉树的层次遍历(bfs)
- Trees on the level 树的层次遍历 UVA 122(BFS)
- 紫书章六例题七 树的层次遍历 UVA 122(链表)
- 例题6-7 二叉树的层次遍历 UVa 122 Trees on the level
- 例题6-7 树的层次遍历(Trees on the level, Duke 1993, UVa 122)
- 树的层次遍历
- 树的层次遍历
- 树的层次遍历
- Codeforces Round #426 (Div. 2) C:The Meaningless Game The Meaningless Game
- ubuntu 16.04 安装pycharm ----通过deb包的方式
- 可持久化。
- 年轻人做好投资
- [SMOJ2073]Bug
- uva-122 树的层次遍历
- web前端学习日记10
- Activity的生命周期
- 方法重写,抽象方法必须重写,抽象类。super.父类里面的方法调用方法。
- 好玩的Raft动画演示,原理秒懂
- HTML CSS:Meeting the Media
- SpringBoot+ActiveMQ多消息队列监听
- Activity的四种启动模式
- JStorm与Storm源码分析(八)--计时器工具-mk-timer