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 ...
随机推荐
- split和join合写
小骆驼 第一章 简介 1. // input 和截取字符 #!usr/bin/perl use strict; use warnings; while ( <> ) { chomp; pr ...
- Java - 记录String中intern()方法的学习与理解
intern()方法:把堆中的引用丢入常量池中,然后返回这个引用.当常量池中已经存在这个引用,就直接返回这个引用.(jdk1.8) 由于jdk1.7中将字符串常量池改为存放在堆中,因此intern() ...
- Python笔记_第四篇_高阶编程_高阶函数_3.sorted
1. sorted函数: 常用的排序分:冒泡排序.选择排序.快速排序.插入排序.计数器排序 实例1:普通排序 # 普通排序 list1 = [,,,,] list2 = sorted(list1) # ...
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之数字类型(number)
Python 数字类型(number)用于存储数值.数据类型是不允许改变的,这就意味着如果改变number数据类型的值,将重新分配内存空间. 1. 一个简单的示例: # 以下实例在变量赋值时数字类 ...
- 股票数据的原始数据形态&数据驱动来设计金融股票业务场景
1. 数据源 其实金融数据没大家想象的那麽复杂,只需要最原始状态的数据,保存到本地即可以. 那麽,怎样才是股票数据的原始状态呢.那就看看1920's年代的道氏理论,他是怎样计算道琼斯指数,那麽他采用的 ...
- FileReader笔记
FileReader API链接地址:https://developer.mozilla.org/en-US/docs/Web/API/FileReader 实例代码: <!DOCTYPE ht ...
- 写一个读取Excel表格的接口
# -*- coding: gbk -*-import xlrd class Canshu: def __init__(self,filepath): """ 创建文件对 ...
- leetcode中的sql
1 组合两张表 组合两张表, 题目很简单, 主要考察JOIN语法的使用.唯一需要注意的一点, 是题目中的这句话, "无论 person 是否有地址信息".说明即使Person表, ...
- 吴裕雄--天生自然Linux操作系统:Linux 简介
Linux 内核最初只是由芬兰人林纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 PO ...
- Win 10 Ctrl + Space 冲突
1. 说明 在IDE里面Ctrl + space 会与 Windows 输入法相互冲突,并且用Ctrl + Space 切换中英文也很不常用(常用直接shift切换). 2. 操作 控制面板——时钟. ...