UVA.122 Trees on the level(二叉树 BFS)

题意分析

给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete

代码总览

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define nmax 10000
using namespace std;
bool failed;
struct node{
bool isvalue;
node * left, *right;
int val;
node():isvalue(false),left(NULL),right(NULL){}
};
node* root;
//node* roott;
char s[nmax];
node* newnode()
{
return new node();
}
bool addnode( int v, char * str)
{
int len = strlen(str);
node*t = root;
for(int i = 0; i<len; ++i){
//if(len == 1)
if(str[i] == 'L'){
if(t->left == NULL) t->left = new node();
t = t->left;
}else if(str[i] == 'R'){
if(t->right == NULL) t->right = new node();
t = t->right;
}
}
if(t->isvalue == true) failed = true;
t->val = v;
t->isvalue = true;
return true;
}
bool read_input()
{
failed = false;
root = newnode();
for(;;){
if(scanf("%s",s) !=1) return false;
else{
int v;
if(!strcmp(s,"()")) break;
sscanf(&s[1],"%d",&v);
addnode(v,strchr(s,',')+1);
}
}
return true;
} bool bfs(vector<int> & ans)
{
queue<node*> q;
ans.clear();
q.push(root);
while(!q.empty()){
node* t = q.front();q.pop();
if(t ->isvalue == false ) {failed = true; break;}
ans.push_back(t->val);
if(t){
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
}
return true; }
int main()
{
//freopen("in.txt","r",stdin);
vector<int> ans;
//roott = newnode();
while(read_input()){
bfs(ans);
if(failed) printf("%s\n","not complete");
else{
bool flag = false;
for(int i = 0; i<ans.size();++i){
if(i == 0) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
}
}
return 0;
}

UVA.122 Trees on the level(二叉树 BFS)的更多相关文章

  1. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  2. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  3. UVa 122 Trees on the level(链式二叉树的建立和层次遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...

  4. UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)

    题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...

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

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  6. 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 ...

  7. UVa 122 Trees on the level

    题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...

  8. 内存池技术(UVa 122 Tree on the level)

    内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...

  9. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

随机推荐

  1. Qt-QML-Button-ButtonStyle-实现鼠标滑过点击效果

    上次实现的自定义的Button功能是用的自定义的Rectangle来实现的,在慢慢的接触了QML之后,发现QML有自己定义的Button 这里盗版贴上Qt帮助文档中的部分关于Button的属性内容 B ...

  2. HTTP请求中get和post的区别是什么

    GET和POST是Http请求中最常用的两种请求方法 首先介绍GET与POST的差异: (1)GET请求资源数据,POST向服务器传递需要处理的数据 (2)GET传递数据大小不超过2kb,POST没有 ...

  3. Android Studio怎样创建App项目

    然后等待大约N分钟: 默认的是Android模式, 改为Project模式更符合我们的习惯:

  4. 使用unittest里面的discover()方法组织测试用例

    import osimport unittest directory = os.getcwd()# 测试用例的目录organize = unittest.defaultTestLoader.disco ...

  5. 第六模块:WEB框架开发 第1章·Django框架开发88~128

    88-Ajax简介 89-Ajax的简单实现 90-基于Ajax的传递数据 91-基于Ajax的登录验证 92-基于Form表单的文件上传 93-请求头之contentType 94-Ajax传递js ...

  6. IntelliJ IDEA 新建项目

    一 新建一个Java项目 二 新建一个Web项目 三 新建一个Maven项目 四 web.xml常见版本 <?xml version="1.0" encoding=" ...

  7. 原生js常用方法

    原生JavaScript设置cookie值 function setCookie(name, value, Hours) { var d = new Date(); var offset = 8; v ...

  8. Centos配置深度学习开发环境

    目录 1. 安装显卡驱动 2. 安装CUDA\CUDNN 3. 安装TensorFlow-gpu 测试 1. 安装显卡驱动 检测显卡驱动及型号 $ sudo rpm --import https:// ...

  9. ThinkPHP - 1 - 本地部署

    ThinkPHP ThinkPHP是一个快速.简单的基于MVC和面向对象的轻量级PHP开发框架,遵循Apache2开源协议发布,从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时 ...

  10. nodejs笔记--基础篇(一)

    Sublime Node.js开发环境配置 下载并安装Node.js安装包后再开始配置 1.先安装好Sublime Text 2 2.运行Sublime,菜单上找到Tools ---> Buil ...