UVA122 Trees on the level

原创 2016年08月28日 22:40:40

题目链接戳这里

题意

这里写图片描述

构造一个二叉树 然后用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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

uva 122 Trees on the level 二叉树的层次遍历(bfs)

题目链接#include #include #include #include #include #include #include #include #include #include #inclu...
  • naipp
  • naipp
  • 2016-08-21 21:27
  • 92

UVa 122 Trees on the level 建立二叉树BFS层序遍历

#include #include #include #include using namespace std; const int maxn = 256 + 10; char s[max...

UVA - 122 Trees on the level

Description Background Trees are fundamental in many branches of computer science. Current...

UVa 122 - Trees on the level

#include #include #include #include using namespace std; const int maxn=550; const int root=1; int c...

UVA 122 Trees on the level 二叉树层次遍历 数组&指针

Trees on the levelTrees are fundamental in many branches of computer science (Pun definitely intende...
  • dl2hq
  • dl2hq
  • 2017-03-18 20:33
  • 131

UVA - 122 Trees on the level

Background Trees are fundamental in many branches of computer science. Current state-of-the art par...

Uva - 122 - Trees on the level

难点在于二叉树的生成和宽度优先遍历BFS,最后要注意防止内存泄漏,虽然题目没有内存限制,但是优化考虑还是做一下释放内存,因为这个不是效率型的问题,测试时间短到没有无法显示。 AC代码: #incl...

UVa122:Trees on the level

题目描述建立节点,并使用BFS输出。代码实现#define _CRT_SECURE_NO_WARNINGS#include #include #define maxn 256using namesp...

[UVA122]Trees on the level

以任意顺序给出一棵二叉树的节点,格式如: (5,LLRL), 数字表示该节点的值. 要求输出一棵树中从一个叶子走到根节点的取到最小权值和的那个叶子节点,假如有多个叶子节点能取到最小的权值,则输出叶子...

UVa 122 Trees on the level

UVa 122 Trees on the level
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)