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上创建RStudio快捷方式
在Linux平台上经常会有一些软件需要通过命令行的方式启动,这没有图标启动方便,下面是在Linux平台为RStudio创建图标链接的方法: 下面以在桌面上创建RStudio快捷方式为例: (1) 首先 ...
- for-each用法误区(不能改变数组元素值)
代码例程: /** * 数据加密传输 */ import java.util.Scanner; public class secretPass { public static void m ...
- php 设计模式之策略者模式
<?php header('Content-Type:text/html;charset=utf-8'); /** * 策略模式演示代码 * * 为了更好地突出“策略”,我们这里以出行为例演示, ...
- LeetCode——714. 买卖股票的最佳时机含手续费.
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交易都需要付手续费.如果你已经购买了一个 ...
- javascript编程中极易出现的错误(个人)
2018-08-10 1,setInterval打错字写成ser 2,document.getElementById().innerHTML;HTML需要全部大写 3,在for循环中定义一个i时要记住 ...
- Python3 Windows服务器简单实现 手机访问
设备 1. PC with Win10 2. Python 3.5.1 步骤 打开CMD,cd 到e盘,mkdir pythonWebserver 建立文件夹 //在E盘建立文件夹,作为服务器的根目录 ...
- c#学习笔记06——XML
XML概述:eXtensible Markup Language,可扩展标记语言.网络应用开发的一项新技术.同HTML一样是一种标记语言,但是数据描述能力要强很多.XML具有描述所有已知未知数据的能力 ...
- Linux--Centos下搭建Git服务器
参考:http://kimi.it/370.html http://blog.csdn.net/wave_1102/article/details/47779401 开始直接用 yum insta ...
- Gson使用指南(二)
注:此系列基于Gson 2.4. 一.Gson的流式反序列化 自动方式 常用的重载方法: Gson.toJson(Object); Gson.fromJson(Reader,Class); Gson. ...
- 多标签图像分类任务的评价方法-mAP
http://blog.sina.com.cn/s/blog_9db078090102whzw.html 多标签图像分类(Multi-label Image Classification)任务中图片的 ...