UVA122 Trees on the level
来源:互联网 发布:java实现接口的方法 编辑:程序博客网 时间:2024/05/23 19:56
题目链接戳这里
构造一个二叉树 然后用bfs层次遍历二叉树
用到sscanf()函数处理字符串
画画图模拟一下很好明白的
用指针写的
/* UVA 122#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <map>#include <set>#define pi acos(-1.0)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18//#define lson l,mid,rt<<1//#define rson mid+1,r,rt<<1|1#define debug(a) printf("---%d---\n", a);#define mem0(a) memset(a, 0, sizeof(a))#define memi(a) memset(a, inf, sizeof(a))#define mem1(a) memset(a, -1, sizeof(a))using namespace std;typedef pair<int, int> P;const double eps = 1e-10;const int maxn = 1e6 + 5;const int mod = 1e8;int failed, node;struct Node{ int val, flag; Node* lson; Node* rson;}tree[maxn]; Node* NewNode(){ Node* p = &tree[node++]; p->flag = p->val = 0; p->lson = p->rson = NULL; return p;}void addnode(Node* root, int v, char* s){ Node* p = root; for (int i = 0; i < strlen(s); i++){ // 从根节点往下走 if (s[i] == 'L'){ if (p->lson == NULL) p->lson = NewNode(); // 结点不存在,建立新结点 p = p->lson; // 往左走 } else if (s[i] == 'R'){ if (p->rson == NULL) p->rson = NewNode(); p = p->rson; } } if (p->flag) failed = 1; // 若此结点已经被赋值 failed p->val = v; p->flag = 1;} void bfs(Node* root, vector<int>* ans){ queue<Node*> q; q.push(root); while (!q.empty()){ Node* p = q.front(); q.pop(); if (p->flag == 0){ failed = 1; } (*ans).push_back(p->val); if (p->lson != NULL) q.push(p->lson); if (p->rson != NULL) q.push(p->rson); }}int main(void){// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin); char s[35], str[35]; int v, i; while (~scanf("%s", s)) { failed = node = 0; Node* root = NewNode(); // 根节点 if (strcmp(s, "()") != 0){ sscanf(s, "(%d,%s", &v, str); // 一种写法 addnode(root, v, str); }else continue; while (~scanf("%s", s)){ if (strcmp(s, "()") == 0) break; sscanf(&s[1], "%d", &v); // 另一种写法 addnode(root, v, strchr(s, ',')+1); } vector<int> ans; if (!failed) bfs(root, &ans); if (failed) puts("not complete"); else { for (i = 0; i < ans.size(); i++) printf("%d%c", ans[i], i+1==ans.size()?'\n':' '); } } return 0;}
不用指针写的
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <map>#include <set>#define pi acos(-1.0)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18//#define lson l,mid,rt<<1//#define rson mid+1,r,rt<<1|1#define debug(a) printf("---%d---\n", a)#define mem0(a) memset(a, 0, sizeof(a))#define memi(a) memset(a, inf, sizeof(a))#define mem1(a) memset(a, -1, sizeof(a))using namespace std;typedef pair<int, int> P;const double eps = 1e-10;const int maxn = 1e6 + 5;const int mod = 1e8;int failed, node;struct Node{ int val, flag; int lson, rson;}tree[maxn];int NewNode(){ tree[++node].val = 0; tree[node].flag = 0; tree[node].lson = 0; tree[node].rson = 0; return node;}void addnode(int rt, int v, char* s){ for (int i = 0; i < strlen(s); i++){ if (s[i] == 'L'){ if (!tree[rt].lson) tree[rt].lson = NewNode(); rt = tree[rt].lson; } else if (s[i] == 'R'){ if (!tree[rt].rson) tree[rt].rson = NewNode(); rt = tree[rt].rson; } } if (tree[rt].flag) failed = 1; tree[rt].val = v; tree[rt].flag = 1;}vector<int> bfs(int rt){ queue<int> q; q.push(rt); vector<int> ans; while (!q.empty()){ int u = q.front(); q.pop(); if (tree[u].flag == 0){ failed = 1; break; } ans.push_back(tree[u].val); if (tree[u].lson) q.push(tree[u].lson); if (tree[u].rson) q.push(tree[u].rson); } return ans;}int main(void){// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin); char s[35], str[35]; int v, i; while (~scanf("%s", s)) { failed = node = 0; int root = NewNode(); // 根节点 if (strcmp(s, "()") != 0){ sscanf(s, "(%d,%s", &v, str); // 一种写法 addnode(root, v, str); }else continue; while (~scanf("%s", s)){ if (strcmp(s, "()") == 0) break; sscanf(&s[1], "%d", &v); // 另一种写法 addnode(root, v, strchr(s, ',')+1); } vector<int> ans; if (!failed) ans = bfs(root); if (failed) puts("not complete"); else { for (i = 0; i < ans.size(); i++) printf("%d%c", ans[i], i+1==ans.size()?'\n':' '); } } return 0;}
0 0
- UVa122 Trees on the level
- uva122-Trees on the level
- uva122 trees on the level
- UVA122 - Trees on the level
- UVA122 Trees on the level
- UVa122:Trees on the level
- [UVA122]Trees on the level
- UVA122 trees on the level(BFS)
- UVA122 Trees on the level【二叉树】【BFS】
- uva122 Trees on the level(锻炼指针建一棵二叉树)
- Trees on the level(UVA122,二叉树的建立)
- Uva122 Trees on the level 二叉树的数组实现
- 例题6-7 UVa122 Trees on the level(树&&队列BFS&&sscanf字符串转整数)
- Uva122 Trees on the level【指针建立树+bfs遍历树】【例题6-7】
- UVA122 Trees on the level(二叉树建立与遍历)
- uva122 Trees on the level (数组,指针两种实现方式)
- TREES ON THE LEVEL
- Trees on the level
- 华为计算字符个数写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
- Div+Css布局相关的属性
- Java笔记--SpringMVC--@RequestMapping
- 怎么做一个MOBA游戏(武器与技能)?
- st算法-延绵的山峰
- UVA122 Trees on the level
- RatingBar的实现
- juery 自定义方法及其调用方式
- spring之旅
- Ubuntu 下安装有道词典
- 适配器模式
- 图解 TCP IP 协议:三次握手、四次挥手
- 转:javascript闭包的理解
- 【用Cocos Creator给暗恋的女生写一个游戏(1)】——Cocos Creator初遇见