例题6-7 树的层次遍历 BFS遍历
来源:互联网 发布:雨田开版软件 编辑:程序博客网 时间:2024/06/05 18:52
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=58&mosmsg=Submission+received+with+ID+20135244
思路:根据输入的点建树,如果有重复赋值的则输出not complete ,然后就是用BFS遍历,把值存入vector容器,如果哪个节点没有值就输出not complete。
Code:
(指针实现)(AC)
#include <iostream>#include <cstdio>#include <vector>#include <queue>#include <string.h>#include <string>using namespace std;const int AX = 256;char s[AX << 2];vector<int>res ;bool flag;struct Node{bool have_value;Node *l , *r;int val;Node():have_value(false),l(NULL),r(NULL){}};Node* root;Node* newNode(){ return new Node(); }/*void remove_tree(Node* u){ //释放内存if( u == NULL ) return ;remove_tree(u->l);remove_tree(u->r);delete u;}*/void addNode( int v , char* s ){int len = strlen(s);Node* u = root ;for( int i = 0 ; 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 -> have_value ) flag = false ; u -> val = v;u -> have_value = true;}bool input_node(){//remove_tree(root);root = newNode();flag = true;while(true){if( scanf("%s",s) != 1 ) return false;if( !strcmp(s,"()" )) return true;int v;sscanf(&s[1],"%d",&v);addNode( v , strchr(s,',') + 1 ) ;}}bool BFS(){queue <Node*> q;res.clear();q.push(root);while( !q.empty() ){Node* u = q.front(); q.pop();if( !u -> have_value ) return false;res.push_back(u -> val);if( u -> l != NULL ) q.push(u->l);if( u -> r != NULL ) q.push(u->r);}return true;}int main(){//freopen("openfile.txt","r",stdin);while(input_node()){if( !BFS() ) flag = false;if(flag){int len = res.size();for( int i = 0 ; i < len-1 ; i++ ){cout << res[i] << ' ';}cout << res[len-1] << endl;}else{ cout << "not complete" << endl;}}return 0;}
数组实现:(TE)数组比指针更慢,暂时不知道如何优化
#include <iostream>#include <cstdio>#include <vector>#include <queue>#include <string.h>#include <string>using namespace std;const int AX = 256;char s[AX << 2];const int root = 1;int left1[AX<<2], right1[AX<<2];bool have_value[AX<<2];int val[AX<<2];int cnt;vector<int>res ;bool flag;void newtree(){left1[root] = right1[root] = 0;have_value[root] = false;}int newNode(){int u = ++cnt;left1[u] = right1[u] = 0;have_value[u] = false;return u;}void addNode( int v , char* s ){int len = strlen(s);int u = root ;for( int i = 0 ; i < len ; i++ ){if( s[i] == 'L' ){if( left1[u] == 0 ) left1[u] = newNode();u = left1[u];}else if( s[i] == 'R' ){if( right1[u] == 0 ) right1[u] = newNode();u = right1[u];}}if( have_value[u] ) flag = false ; val[u] = v;have_value[u] = true;}bool input_node(){cnt = 0;newtree();flag = true;while(true){if( scanf("%s",s) != 1 ) return false;if( !strcmp(s,"()" )) return true;int v;sscanf(&s[1],"%d",&v);addNode( v , strchr(s,',') + 1 ) ;}}bool BFS(){queue <int> q;res.clear();q.push(root);while( !q.empty() ){int u = q.front(); q.pop();if( !have_value[u] ) return false;res.push_back(val[u]);if( left1[u] != 0 ) q.push(left1[u]);if( right1[u] != 0 ) q.push(right1[u]);}return true;}int main(){//freopen("openfile.txt","r",stdin);while(input_node()){if( !BFS() ) flag = false;if(flag){int len = res.size();for( int i = 0 ; i < len-1 ; i++ ){cout << res[i] << ' ';}cout << res[len-1] << endl;}else{ cout << "not complete" << endl;}}return 0;}
阅读全文
0 0
- 例题6-7 树的层次遍历 BFS遍历
- 树得层次遍历@例题6-7
- 例题6-7 树的层次遍历 UVa122
- 例题 6-7 树的层次遍历 UVa 122 建树+宽度优先遍历
- 例题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)
- 数据结构:树的BFS,树的层次遍历! 按先序遍历创建一棵树,然后以层次遍历输出。
- 白书练习 层次遍历 (二叉树的bfs)
- 剑指offer--二叉树的深度--层次遍历/BFS
- BFS算法(类似与树的层次遍历)
- 树的层次遍历
- 树的层次遍历
- 树的层次遍历
- 树的层次遍历
- 树的层次遍历
- 树的层次遍历
- 树的层次遍历
- 401 Palindromes(回文串与镜像串)
- The "data" option should be a function that returns a per-instance value in component definitions.
- css复习——opacity(不透明度)、IE6固定定位、border-radius(圆角)
- Java中"=="和equals方法的区别
- python里使用正则表达式排除集合里字符的功能
- 例题6-7 树的层次遍历 BFS遍历
- 第二章 在HTML中使用JavaScript
- POJ 3581 Sequence 笔记
- 遗传算法简单介绍与MATLAB实现(三)
- android studio创建横屏layout-land
- 原来Github上的README.md文件这么有意思——Markdown语言详解
- DFS + 高斯消元 HDU 5544
- Flask Web开发(一)基于python的Web表单
- 从零开始到搭建hexo个人博客