题目链接:

https://cn.vjudge.net/problem/UVA-122

 /*
问题
给出每个节点的权值和路线,输出该二叉树的层次遍历序列。 解题思路
根据输入构建链式二叉树,再用广度优先遍历保存权值最后输出。
*/
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
bool failed; struct NODE{
bool have_value;
int v;
NODE *left,*right;
NODE() : have_value(false),left(NULL),right(NULL){};
};
NODE* newnode(){
return new NODE();
}
NODE* root; bool read_input();
void addnode(int v,char *s);
bool bfs(vector<int> &ans);
void remove_tree(NODE* u){
if(u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
delete u;
} int main()
{
//freopen("E:\\testin.txt","r",stdin);
vector<int> ans;
while(read_input()){
if(failed || !bfs(ans))
printf("not complete\n");
else{
int i;
for(i=;i<ans.size()-;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[i]);
}
}
return ;
} 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;
}
void addnode(int v,char *s){
int len=strlen(s); NODE* u= root;
for(int i=;i<len;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;
}
bool read_input(){
char s[maxn];
failed=false;
remove_tree(root);
root=newnode();
for(;;){
if(scanf("%s",s) != ) return false;
if(!strcmp(s,"()")) break;
int v;
sscanf(s+,"%d",&v);
addnode(v,strchr(s,',')+);
}
return true;
}

UVa 122 Trees on the level(链式二叉树的建立和层次遍历)的更多相关文章

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

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

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

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

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

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

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

  5. UVa 122 Trees on the level

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

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

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

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

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

  8. 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历

    给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7],    3   ...

  9. Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历

    给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / ...

随机推荐

  1. SRM468

    250pt 给定手机0-9按键对应的英文字母(1个对多个),0固定对应空格.然后在给定一些单词.以及一个要处理的串,叫你按照那个串模拟输出结果 思路: 大模拟,写的有点乱 // BEGIN CUT H ...

  2. 微擎 人人商城 merchant.php源码

    <?php define('IN_SYS', true); require '../framework/bootstrap.inc.php'; load()->web('common'); ...

  3. C#如何在List里求某一列的數值的和SUM

    var X=Xlist.Sum(key => key.XXX);

  4. 背水一战 Windows 10 (45) - 控件(图标类): IconElement, SymbolIcon, FontIcon, PathIcon, BitmapIcon

    [源码下载] 背水一战 Windows 10 (45) - 控件(图标类): IconElement, SymbolIcon, FontIcon, PathIcon, BitmapIcon 作者:we ...

  5. 浅谈react受控组件与非受控组件

    引言 最近在使用蚂蚁金服出品的一条基于react的ant-design UI组件时遇到一个问题,编辑页面时input输入框会展示保存前的数据,但是是用defaultValue就是不起作用,输入框始终为 ...

  6. VMware中安装Contos

    1 检查BIOS虚拟化支持 2 新建虚拟机 3 新建虚拟机向导 4 创建虚拟空白光盘 5 安装Linux系统对应的CentOS版 6 虚拟机命名和定位磁盘位置 7 处理器配置,看自己是否是双核.多核 ...

  7. Flash 0day漏洞(CVE-2018-4878)复现

    该漏洞影响 Flash Player 当前最新版本28.0.0.137以及之前的所有版本,而Adobe公司计划在当地时间2月5日紧急发布更新来修复此漏洞. 本文作者:i春秋作家——F0rmat 前言 ...

  8. 抓取分析网页批量下载评书(3)之批量下载mp3

         本系列目录:    <1.搜索有声小说>    <2.分析详细页地址>     <3.批量下载mp3>      本篇是大结局,看过前两篇的放心吧,不会有 ...

  9. css3中那些鲜为人知但又很有用的属性

    概述 这是我在写移动端页面的时候遇到的,css3中鲜为人知但又很有用的属性,记录下来,供以后开发时参考,相信对其他人也有用. tap-highlight-color 在移动端开发中,我们需要在用户轻按 ...

  10. jQuery的ajax的post请求json格式无法上传空数组

    问题描述:在和后端对接时,使用jquery的ajax的post方式向后端传递一序列约定好格式的对象数组.遇到了一个现象:如果对象中的数组是空数组,那么在请求参数中是不会出现的. 以下是数据的对比:   ...