立志用最少的代码做最高效的表达


Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateof-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics.
This problem involves building and traversing binary trees.
Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.
In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k + 1.
For example, a level order traversal of the tree on the right is: 5, 4, 8, 11, 13, 4, 7, 2, 1.
In this problem a binary tree is specified by a sequence of pairs ‘(n,s)’ where n is the value at the node whose path
from the root is given by the string s. A path is given be a sequence of ‘L’s and ‘R’s where ‘L’ indicates a left branch and ‘R’ indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.

Input
The input is a sequence of binary trees specified as described above. Each tree in a sequence consists
of several pairs ‘(n,s)’ as described above separated by whitespace. The last entry in each tree is ‘()’.
No whitespace appears between left and right parentheses.
All nodes contain a positive integer. Every tree in the input will consist of at least one node and
no more than 256 nodes. Input is terminated by end-of-file.

Output
For each completely specified binary tree in the input file, the level order traversal of that tree should
be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a
node is given a value more than once, then the string ‘not complete’ should be printed.

Sample Input
(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output
5 4 8 11 13 4 7 2 1
not complete


分析

输入数据,将数字和字符筛选出来,字符根据函数生成子树,同时将数字赋给该节点。
在建树的时候设置一个bool类型,置0,遍历到就置1。

两种错误类型:

1、若遍历到1,就说明已经遍历过了,表明输入了重复的根节点
2、最后输出时判断,若有节点但没被赋值过,表明输入了重复的非根节点

一道很好的层序遍历练习题


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;const int maxn = 256+5;
char s[maxn];                   //保存读入的节点
bool failed;//二叉树的节点定义和操作。定义一个Node结构体,对应整棵二叉树根root
//如果要定义一颗二叉树,一般是定义struct,然后保存树根的指针(Node* root)
/*
每次需要一个新的Node时,都要用new运算符申请内存,并执行构造函数
下面把申请新节点的操作封装到newnode中
*/
struct Node {bool have_value;       //是否被赋值int v;                   //节点值Node *left, *right;    Node() : have_value(false), left(NULL), right(NULL) {}
};
Node* root;         //以后的遍历操作都是基于这个根 //可以使用new运算符申请空间并执行构造函数,如果返回值为null,说明
//空间不足,申请失败
Node* newnode() {   //申请新节点 return new Node();
} /*
接下来是在read_input中调用的addnode函数。它按照移动序列行走,目标
不存在时调用newnode创建新节点
*/
void addnode(int v, char* s) {int n = strlen(s);Node* u = root;           //从根节点开始往下走 for(int i = 0; i < n; i++) {if(s[i] == 'L') { //如果节点不存在,则创建新节点 if(u->left == NULL) u->left = newnode();u = u->left;} else if(s[i] == 'R') {if(u->right == NULL) u->right = newnode();u = u->right;}}      if(u->have_value) failed = true;   //如果已经赋过值,则表明输入有误u->v = v;u->have_value = true;
}//以上,输入、建树结束,接下来BFS层序遍历
bool bfs(vector<int>& ans) {      //ans存储层序遍历的节点值 queue<Node*>q;ans.clear();q.push(root);while(!q.empty()) {Node* u = q.front(); q.pop();if(!u->have_value) return false;   //有节点没有被赋值过,表明输入有误ans.push_back(u->v);if(u->left != NULL) q.push(u->left); //把左子节点放进队列if(u->right != NULL) q.push(u->right); } return true;     //输入正确
}/* */
bool read_input() {failed = false;root = newnode();           //创建根节点 //C语言的灵活性,可以灵活选取字符串的值作为“首地址”传输 for(;;) {if(scanf("%s", s) != 1) return false; //输入结束if(!strcmp(s, "()")) break;             //读到结束标志,退出循环int v;sscanf(&s[1], "%d", &v);            //读入节点值(从1开始查找)addnode(v, strchr(s, ',')+1);     //查找逗号,然后插入节点 } return true;
} /*
void remove_tree(Node* u) {if(u == NULL) return;      //提前判断比较稳妥 remove_tree(u->left);     //递归释放左子树的空间 remove_tree(u->right);      //递归释放右子树的空间 delete u;                  //调用u的析构函数并释放u节点本身的内存
}
*/int main() {while(read_input()) {if(failed == true) {       printf("not complete\n");continue;} vector<int>v;if(bfs(v)) {for(int i = 0; i < v.size(); i++) {printf("%d%s", v[i], i==v.size()-1?"\n":" ");}} else {printf("not complete\n");}}return 0;
} 

【可运行,刘汝佳代码】Trees on the level UVA - 122相关推荐

  1. 【刘汝佳代码详解】例题6-4破损的键盘(Broken Keyboard,UVa 11988)

    立志用最少的代码做最高效的表达 You're typing a long text with a broken keyboard. Well it's not so badly broken. The ...

  2. Trees on the level UVA - 122 (二叉树的层次遍历)

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  3. 算法竞赛入门经典(刘汝佳)——代码笔记

    Reference: <算法竞赛入门经典>(刘汝佳)第一版.第二版 ------------------------------------------------------------ ...

  4. 刘汝佳《算法竞赛入门经典(第二版)》习题(六)

    刘汝佳<算法竞赛入门经典(第二版)>第四章习题(4-1~4-3) 习题4-1 象棋(Xiangai,ACM/ICPC Fuzhou 2011,UVa1589) 考虑一个象棋残局,其中红方有 ...

  5. 刘汝佳《算法竞赛入门经典》---总结

    刘汝佳:<算法竞赛入门经典> 三步: 基本的数据结构+算法知识: 数论等数学基本知识: 锻炼联想建模能力.知识与实际相结合,解决实际问题! 第一章:程序设计入门 1.a/b 当a.b为整数 ...

  6. 刘汝佳紫书 uva1592

    刘汝佳上传的代码没耐心看...就大致照着书上写的思路写了一个...一开始发现全写错了耗了半天... 定义一个pair,这个pair里有存两个队,字符串对和int对.每每读入一行的时候看看map中是否存 ...

  7. 刘汝佳《算法竞赛入门经典(第二版)》习题(三)

    刘汝佳<算法竞赛入门经典(第二版)>第三章习题(一) 习题3-1 得分(ACM/ICPC Seoul 2005,UVa1585) 给出一个由O和X组成的串(长度为1~80),统计得分.每个 ...

  8. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  9. 【刘汝佳】习题3-1 UVA1585

    刘汝佳 <算法竞赛入门经典(第二版)>第三章习题 3-1 习题3-1 得分(ACM/ICPC Seoul 2005,UVa1585) 给出一个由O和X组成的串(长度为1~80),统计得分. ...

最新文章

  1. 开源中国app说什么 旁边的那个图标是什么drawable
  2. 央视在世界杯高清直播中占了C位 它是怎么做到的?
  3. 乡镇银行和信用社哪个正规?
  4. 多系统通讯-DotNetMQ
  5. python database ioerror_python – IOError:[Errno 2]没有这样的文件或...
  6. 【C++】C++类的学习(一)——初识类
  7. [Flink] Not a valid protocol version This 1s not an HTTP port
  8. VS2003/VS2005 常用快捷键
  9. 安卓绿色联盟安全标准1.0到2.0,让用户隐私更安全
  10. [Python3] 010 字符串:给你们看看我的内置方法 第二弹
  11. 相关滤波的视觉目标跟踪算法学习
  12. iOS 禁止横屏的解决方案
  13. php怎么更换图片背景的软件,如何更换照片背景
  14. Baizhi Memcached GJF
  15. 微信小程序定制开发的几大类型
  16. 【亲测 | 007】易搜资料 V2.4.3版本 非独立版 亲测无错 | 极致技术工厂
  17. 点亮科技树——锂离子电池
  18. 【零基础学Python】爬虫篇 :第十四节--爬虫+词云解决实际问题
  19. react native 调用手机内置地图
  20. Hotsopt对象探秘

热门文章

  1. Redis 过期键删除策略、内存淘汰机制
  2. Linux 权限管理: 权限的概念、权限管理、文件访问权限的设置、 粘滞位
  3. 设计模式:责任链模式(Chain of Responsibility)
  4. 《微服务架构设计模式》总结,文末送书
  5. Django静态文件处理、中间件及Admin站点
  6. 音视频技术开发周刊 | 190
  7. 当一百万名记者都嚷嚷着“Facebook 很糟糕”......
  8. 5G时代探索互动立体视频信息承载的新可能
  9. 基于FPGA的异构计算在多媒体中的应用
  10. C/C++学习之路_七: 内存管理