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
  • 118

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 state...

例题6-7 UVa122 Trees on the level(树&&队列BFS&&sscanf字符串转整数)

题意: 看白书 要点: 这题还真难,有很多知识不说,还有很多C++的容器。结点最多256个,要考虑最差的情况就是排成一条链,这样深度是256,如果用数组会爆炸,所以用动态分配。 #include ...

Uva122 Trees on the level 二叉树的数组实现

作为一个还没有系统学习数据结构知识的大一学生,简单谈一下通过这道题对二叉树产生的认识。       二叉树的一个节点中起码包含三个基础信息:节点权值、左子节点的地址(或编号)、右子节点的地址(或编号...
  • jijijix
  • jijijix
  • 2016年08月12日 18:11
  • 154

例题6-7 UVA 122 - Trees on the level 树的层次遍历

因为过年,这个题看了比较长的时间,但是看懂了后,感觉题目解析非常巧妙 这篇博客就记录下自己学到的东西吧! 1。在输入方面,直接开了一个bool read_input()函数,这样在输入EOF时,就...

UVA.122 Trees on the level(二叉树 BFS)

UVA.122 Trees on the level(二叉树 BFS)题意分析给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete代码总览#include #in...

UVA 122 && HDU 1622 Trees on the level(构造二叉树+BFS层序遍历)

Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...

UVa 122 Trees on the level , Duke 1993

题目: Trees are fundamental in many branches of computer science (Pun definitely intended). Curren...

UVa Problem 122 - Trees on the level

// UVa Problem 122 - Trees on the level // Verdict: Accepted // Submission Date: 2011-12-25 // UVa R...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA122 Trees on the level
举报原因:
原因补充:

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