题解:

我的解法是用一个类似字典树结构的结构体来表示节点。看到另一种解法是用数组来映射二叉树的,开到14000就过了,但是我觉得是数据水了,因为题中说最多 256个节点,如果256个节点连成链型,除根节点外每个节点都是父节点的右儿子。那么数组要开pow(2, 256)个。可见这种方法是不可行的;

  • 类似字典树的二叉树

    Accepted 1167 C++ 0 280
    #include "cstdio"
    #include "cstring"
    #include "cstdlib"
    #include "cctype"
    #include "queue"
    #include "vector"
    using namespace std;
    struct Tree {
    int data;
    Tree* lson;
    Tree* rson;
    } *root;
    char s[];
    // v记录遍历的结果,ok记录可否构成树
    vector<int> v;
    bool ok;
    // 初始化节点
    Tree* init() {
    Tree* point = (Tree*)malloc(sizeof(Tree));
    point->data = ;
    point->lson = point->rson = NULL;
    return point;
    }
    // 插入一个节点
    void insert(char* s) {
    int _data = , i = ;
    Tree* point = root;
    while (isdigit(s[i])) {
    _data = _data * + (s[i++] ^ '');
    }
    i++;
    while (s[i] != ')') {
    if (s[i++] == 'L') {
    if (point->lson == NULL) {
    point->lson = init();
    }
    point = point->lson;
    } else {
    if (point->rson == NULL) {
    point->rson = init();
    }
    point = point->rson;
    }
    }
    if (point->data) {
    ok = false;
    }
    point->data = _data;
    }
    // 按层遍历并释放二叉树
    void BFS() {
    queue<Tree*> q;
    q.push(root);
    Tree* point;
    while (!q.empty()) {
    point = q.front();
    q.pop();
    v.push_back(point->data);
    if (!point->data) {
    ok = false;
    }
    if (point->lson) {
    q.push(point->lson);
    }
    if (point->rson) {
    q.push(point->rson);
    }
    free(point);
    }
    }
    int main() {
    while (~scanf("%s", s)) {
    root = init();
    v.clear();
    ok = true;
    while(s[] != '\0') {
    insert(s);
    scanf("%s", s);
    }
    BFS();
    if (!ok) {
    puts("not complete");
    continue;
    }
    printf("%d", v[]);
    for (int i = ; i < v.size(); i++) {
    printf(" %d", v[i]);
    }
    puts("");
    }
    return ;
    }

ZOJ-1167-Trees on the Level的更多相关文章

  1. E - Trees on the level

     Trees on the level  Background Trees are fundamental in many branches of computer science. Current ...

  2. Trees on the level(指针法和非指针法构造二叉树)

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

  3. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

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

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

  5. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

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

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  7. uva 122 trees on the level——yhx

    题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...

  8. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  9. 【ZOJ】3740:Water Level【DP】

    Water Level Time Limit: 2 Seconds      Memory Limit: 65536 KB Hangzhou is a beautiful city, especial ...

  10. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

随机推荐

  1. Thread--使用condition实现顺序执行

    package condition; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Re ...

  2. D11 列表 list 元祖 字典dict

    取值 name = "alexdfg" print(name[3:5]) 取出 ex name = "alexdfg" print(name[3]) 取出e 列 ...

  3. Aras Innovator获取项目任务序列号

    //方法名:GetProjectTasksNumber //功能描述:获取项目任务序列号 //原作者:joe //创建时间:20141225 //版权所有(C)JOE.FAN //---------- ...

  4. 取石子游戏(gcd)

    蒜头君和花椰妹在玩一个游戏,他们在地上将n颗石子排成一排,编号为1到n.开始时,蒜头君随机取出了2颗石子扔掉,假设蒜头君取出的2颗石子的编号为a, b.游戏规则如下,蒜头君和花椰妹2人轮流取子,每次取 ...

  5. java线程——线程基础

    一,线程之间的关系 线程之间存在两种关系: (1)间接相互制约:相互争夺线程资源: (2)直接相互制约:线程之间的相互合作: 间接相互制约也可以成为互斥,直接相互制约也可以称为同步:同步也包括互斥,互 ...

  6. 提高WiFi上网速度

    https://jingyan.baidu.com/article/1876c852aa668c890b1376c4.html http://www.coozhi.com/youxishuma/you ...

  7. python中的倒序遍历

    1.在列表本身倒序 a = [1, 3, 7, 5, 2, 6] a.reverse() # 在列表本身进行倒序,不返回新的值 print(a) # 输出a: # [6, 2, 5, 7, 3, 1] ...

  8. Python cannot import name 'Line' from 'pyecharts'

    问题与尝试 代码 from pyecharts.charts import Line 中,出现 cannot import name 'Line' from 'pyecharts' 错误. 找了很多, ...

  9. ajax使用json数组------前端往后台发送json数组及后台往前端发送json数组

    1.引子 Json是跨语言数据交流的中间语言,它以键/值对的方式表示数据,这种简单明了的数据类型能被大部分编程语言理解.它也因此是前后端数据交流的主要方式和基础. 2.前端往后台传输json数据 第一 ...

  10. Codeforces 1294B - Collecting Packages

    题目大意: 机器人从(0,0)开始,他只能往上'U'或者往右'R'走 坐标系中有着很多包裹,分别在一些点上 机器人需要走过去把这些包裹全部收集起来 问能不能做到 如果能,再输出移动方式,相同移动方式输 ...