树的层次遍历(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当作布尔型 ...
随机推荐
- mac os 隐藏文件夹的小技巧
无论是谁,电脑里总有些不想让人看到的内容,或是私密日记,或是某播下载的奇怪东西,对于这些东西当然是不想被人看到的.怎么办呢? 有人说了几种方法: 1. 改名字: 2. 把文件夹做成加密DMG: 3. ...
- Python 学习笔记(十)Python集合(二)
集合常用的方法 add() 向集合中增加一个元素,如果集合中已经有了这个元素,那个这个方法就会失效 >>> help(set.add) Help on method_de ...
- Docker 三种UI管理平台
docker集中化web管理平台 一.shipyard 1.启动docker,下载镜像 # systemctl restart docker # docker pull alpine # docker ...
- Unity 游戏框架搭建 (二十二) 简易引用计数器
引用计数是一个很好用的技术概念,不要被这个名字吓到了.首先来讲讲引用计数是干嘛的. 引用计数使用场景 有一间黑色的屋子,里边有一盏灯.当第一个人进屋的时候灯会打开,之后的人进来则不用再次打开了,因为已 ...
- 禁用Linux透明大页
Oracle 安装时官方建议关闭Linux的透明大页,防止内存延迟分配导致的性能问题 https://docs.oracle.com/cd/E11882_01/install.112/e47689/p ...
- mysqld_safe之三言两语
today,one buddy in IMG wechat group 2 asked "why i've installed the MySQL 5.7 on linux serv ...
- HTML+css 文字只显示一行
电脑端 设置行高,超出隐藏 p{ width: 80%; height: 16px; line-height: 16px; display: block; overflow: hidden; text ...
- 配置一个nginx反向代理&负载均衡服务器
一.基本信息 系统(L):CentOS 6.9 #下载地址:http://mirrors.sohu.com 反代&负载均衡(N):NGINX 1.14.0 #下载地址:http://nginx ...
- yii学习笔记(4),获取请求数据的request组件
yii在控制器中获取请求数据需要通过request组件来完成 <?php namespace app\controllers; use yii; use yii\web\Controller; ...
- ajax重定向登录页
/** * ajax默认设置 * 包括默认提交方式为POST, * 判断后台是否是重定向 */ $.ajaxSetup( { //设置ajax请求结束后的执行动作 complete : functio ...