树的层次遍历(Trees on the level,UVA 122)
题目描述:

题目思路:
1.用结构链表来建树
2.用队列来实现层次遍历,当遍历到根节点时,将其子节点压入队列
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
using namespace std; //树结点
struct Node{
int v ;
Node* left,*right ;
int have_value ;
Node():have_value(false),left(NULL),right(NULL){} ;
} ; Node* root ;//根节点 Node* newnode(){
return new Node() ; //返回一个新结点
} bool failed ; void addnode(int v,char* s){//添加新结点
int n = strlen(s);
Node* u = root ;
for(int i = ;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;
} void freetree(Node* u){ //释放内存
if(u == NULL) return ;
freetree(u->left);
freetree(u->right);
delete u;
} char s[];
bool read_input(){
failed = false ;
freetree(root) ;
root = newnode();
while(true){
if(scanf("%s", s) != ) return false;
if(!strcmp(s,"()")) break;
int v ;
sscanf(&s[],"%d",&v);
addnode(v,strchr(s,',')+);
}
return true ;
} bool bfs(vector<int>& 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 ;
} int main(int argc, char *argv[])
{
vector<int> ans;
while(read_input()){
if(!bfs(ans)) failed = ;
if(failed) printf("not complete\n");
else{
for(int i = ;i < ans.size();i++)
{
if(i != ) cout << " " ;
cout << ans[i];
}
cout << endl ;
}
}
return ;
}
树的层次遍历(Trees on the level,UVA 122)的更多相关文章
- Uva 122 树的层次遍历 Trees on the level lrj白书 p149
是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担 ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- Trees on the level UVA - 122 (二叉树的层次遍历)
题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...
- UVa 122 (二叉树的层次遍历) Trees on the level
题意: 输入一颗二叉树,按照(左右左右, 节点的值)的格式.然后从上到下从左到右依次输出各个节点的值,如果一个节点没有赋值或者多次赋值,则输出“not complete” 一.指针方式实现二叉树 首先 ...
- Trees on the level UVA - 122
Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateo ...
- 【紫书】Trees on the level UVA - 122 动态建树及bfs
题意:给你一些字符串,代表某个值被插入树中的位置.让你输出层序遍历. 题解:动态建树. 由于输入复杂,将输入封装成read_input.注意输入函数返回的情况 再将申请新节点封装成newnode(). ...
- [Swift]LeetCode103. 二叉树的锯齿形层次遍历 | Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [Swift]LeetCode107. 二叉树的层次遍历 II | Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 二叉树的锯齿形层次遍历 · Binary Tree Zigzag Level Order Traversal
[抄题]: 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) [思维问题]: 不知道反复切换要怎么做:用boolean normalOrder当作布尔型 ...
随机推荐
- 使用PIE.htc让万恶的IE内核浏览器IE6\7\8支持CSS3部分属性
万恶的IE内核浏览器,这是多少前端程序员头疼的事情... 今天给大家介绍一下如何用 PIE.htc 来让IE浏览器支持CSS3的 border-radius.box-shadow.CSS3 Backg ...
- 【oracle笔记1】基础知识大集锦:增删改,数据类型,用户操作,持续更新中···
什么是数据库?数据库就是用来存储和管理数据的仓库.首先我来简单介绍一下各数据库的背景,常见的数据库如下,oracle:甲骨文公司(市场占用率最高),oracle也是一个公司名,翻译过来就是甲骨文的意思 ...
- iOS之safari调试iOS app web页面
Overview 当下移动端开发过程中大量使用前段H5.js等等技术,而这些web页面的调试在Xcode控制台中不太明了,经常我们移动app运行了就是方法,但是不能显示响应的效果,这时候或许就是已经报 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池
DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...
- 搭建Extjs框架(一)
搭建Extjs框架 pc端 github https://github.com/Status400/Extjs-6.2.0-demo 欢迎start 准本工作: 官方下载Extjs ...
- 分布式网上商城项目- BeanDefinitionStoreException
BeanDefinitionStoreException: 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanDe ...
- jquery 去除空格
/** * 是否去除所有空格 * @param str * @param is_global 如果为g或者G去除所有的 * @returns */ function Trim(str,is_globa ...
- ASP.NET Core优化MD5加密
MD5是我们常用的一种加密方式,但是有朋友和我说C#自带的MD5方法碰撞阻力太低,担心安全问题 然后我这里开源一下我日常使用的优化后的MD5加密方法 代码中先创建出MD5对象后对字符串先进行MD5加密 ...
- mqtt使用一
最近做的一个项目用到了mqtt协议,我需要从第三方订阅主题接受消息,还需要自己搭建,mqtt服务器去发布主题.下面就详细介绍一下环境的搭建和使用. 1.mqtt介绍 MQTT是一个基于客户端-服务器的 ...
- 【Android】添加依赖包
貌似好像不知一种方法,以后有时间再研究,下面是其中的一种方法