ZOJ-1167-Trees on the Level
题解:
我的解法是用一个类似字典树结构的结构体来表示节点。看到另一种解法是用数组来映射二叉树的,开到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的更多相关文章
- E - Trees on the level
Trees on the level Background Trees are fundamental in many branches of computer science. Current ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1622 Trees on the level(二叉树的层次遍历)
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- 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 ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- 【ZOJ】3740:Water Level【DP】
Water Level Time Limit: 2 Seconds Memory Limit: 65536 KB Hangzhou is a beautiful city, especial ...
- 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 ...
随机推荐
- Linux--Shell传递参数
参考:http://www.runoob.com/linux/linux-shell-passing-arguments.html
- JavaScript—面向对象贪吃蛇_1
前面说了.面向对象的思考方式和面向过程的思考方式有着本质的区别. 贪吃蛇.作为各大培训机构.面向对象的练手项目,的确好.我昨天看完视频,有一种领悟面向对象的感觉,当然可能只针对贪吃蛇..要想在实际开发 ...
- Django2.0——Form组件简单总结
Django提供了一个Form组件来配和前端的表单进行使用,Form有两个强大的功能,分别是生成HTML代码和验证数据的合法性.通常我们不会用其第一个功能,因为前端的设计可以做出更加精美且多样的表单页 ...
- 挑战目标跟踪算法极限,SiamRPN系列算法解读
商汤科技智能视频团队首次开源其目标跟踪研究平台 PySOT.PySOT 包含了商汤科技 SiamRPN 系列算法,以及刚被 CVPR2019 收录为 Oral 的 SiamRPN++.此篇文章将解读目 ...
- 视图家族之mixins视图工具类与generics工具视图类
视图家族之mixins视图工具类与generics工具视图类 一.mixins视图工具类 作用: 提供了几种后端视图(对数据资源进行曾删改查)处理流程的实现,如果需要编写的视图属于这五种,则视图可以通 ...
- 系统学习python第二天学习笔记
1.对day01所学内容的练习 """ 评分规则: A >=90 B >=80 C >=70 D 其他 用户输入成绩,根据成绩的不同显示不同的级别. & ...
- Thinkcmf截取内容长度
例1: {$vo.post_title|msubstr=0,10} 截取标题,msubstr=0,10,数字表示截取的字符串长度,显示省略号,但无论长度是否超过截取的长度都会出现省略号: 例2: {$ ...
- UVA 10801 多线程最短路
题意:一栋摩天大楼从0层到K层,有N部电梯,每个电梯都有自己的运行速度,此外,对于某个电梯来说,并不是每一层都会停,允许在某一层进行电梯换乘,每次换乘固定消耗60秒,最终求从0层去K层的最短时间,如果 ...
- 关于SpringMVC的使用总结
简介 springMVC即Spring Web MVC,是spring web模块的一部分,是spring自己的web框架 springMVC对Servlet API 进行了完善的封装,极大的简化了开 ...
- CCPC2019网络赛
2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A 题意:找到最小的正整数 C 使得 (A^C)&(B^C) 最小. \(A,B \le 10^9\) 签到题.这个C取 A& ...