题解:

我的解法是用一个类似字典树结构的结构体来表示节点。看到另一种解法是用数组来映射二叉树的,开到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. SQL基础教程(第2版)第5章 复杂查询:5-3 关联子查询

    第5章 复杂查询:5-3 关联子查询 ● 关联子查询会在细分的组内进行比较时使用.● 关联子查询和GROUP BY子句一样,也可以对表中的数据进行切分.● 关联子查询的结合条件如果未出现在子查询之中就 ...

  2. render_template()的各种用法

    1.可以有很多个参数,第一个一定是模板的名字 2.可以传字典.列表.单个变量等等,还可以传函数,在模板中调用函数 后端函数: from flask import Flask from flask im ...

  3. 学习SEO之7天精通SEO

    这本书大致看了一下,对于SEO基本上有了一个初步的认识,附上链接以供学习之用. 百度网盘:https://pan.baidu.com/s/1Bntzh2YF4tBd2AYAL1Q8vQ 心得:1.SE ...

  4. Linux系统提示无法获得锁

    这种情况出现主要是因为软件更新或者安装时出现错误. 删除掉两个临时文件即可 sudo rm /var/lib/dpkg/lock sudo rm /var/cache/apt/archive/lock ...

  5. atomic一定线程安全吗

    atomic只是保证了操作的原子性,原子操作即一个操作不可再分. atomic只是对读写操作进行了加锁,保证了多线程开发时一个读写操作完成之后才能进行下一个读写操作 atomic和线程安全没有太大的关 ...

  6. python全局灰度线性变换——自由设定图像灰度范围

    全局线性变换的公式是s = (r-a)*(d-c)/(b-a)+c,其中a.b是原图片的灰度最小值和最大值,c.d是变换后的灰度值的最小值和最大值.r是当前像素点的灰度值,s是当前像素点变换后的灰度值 ...

  7. 3.docker machine 连接 aliyun 远程docker 服务器

    1.在aliyun ecs 创建docker 服务器 docker-machine create -d aliyunecs machine-aliyunecs 2.远程连接 docker 获取客户端 ...

  8. 计量经济与时间序列_关于Box-Jenkins的ARMA模型的经济学意义(重要思路)

    1 很多人已经了解到AR(1)这种最简单的时间序列模型,ARMA模型包括AR模型和MA模型两个部分,这里要详细介绍Box-Jenkins模型的观念(有些资料中把ARMA模型叫做Box-Jenkins模 ...

  9. js实现新闻滚动-单行滚动或者多行滚动

    注明:都是转载. 先说单行滚动: --------直接复制以下代码即可试验 转载http://www.3lian.com/edu/2011/06-30/4986.html----------- < ...

  10. gulp自动化添加版本号并修改为参数格式

    问题: 当我们修改js和css文件时往往需要清除浏览器的缓存,否则有些效果就看不到更新过后的. 通过对js,css文件内容进行hash运算,生成一个文件的唯一hash字符串(如果文件修改则hash号会 ...