题目链接:

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. Javascript 对象继承 原型链继承 对象冒充 call 混合方式

    一.原型链继承 function ClassA() {} ClassA.prototype.color = "blue"; ClassA.prototype.sayColor = ...

  2. DXP 板层

    一)DXP-设置板层(D+K )在PCB编辑 Design->Layer Stack Manager(层管理) 1)快捷命令 D + K   进入么多层置管理器 2.鼠标右键 TopLayer- ...

  3. WPF 使用 Direct2D1 画图 绘制基本图形

    本文来告诉大家如何在 Direct2D1 绘制基本图形,包括线段.矩形.椭圆 本文是一个系列 WPF 使用 Direct2D1 画图入门 WPF 使用 Direct2D1 画图 绘制基本图形 本文的组 ...

  4. NTLM移除

  5. 安装fastdfs文件系统

    1.下载源码包 需要下载的源码包: fastDFS源代码:FastDFS_v5.01.tar.gz fastDFS的nginx模块源代码:fastdfs-nginx-module_v1.15.tar. ...

  6. underscore.js源码研究(5)

    概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...

  7. 【dpdk】使用libpcap-PMD驱动收发包

    ref: Dpdk programmer’s guide 1.  概述 dpdk不仅提供针对物理和虚拟网卡的pmd驱动(Poll Mode Drivers),还提供两个纯软件的pmd驱动,libpca ...

  8. Synchronzied(内置锁)

    原文地址:深入JVM锁机制1-synchronized 1. 线程的状态与转换 当多个线程同时请求某个对象监视器时,对象监视器会设置几种状态用来区分请求的线程: Contention List:所有请 ...

  9. [工具]Cobalt Strike 3.13 TeamServer for Windows

    Cobalt Strike 3.13 TeamServer for Windows 0x001 环境 CS 3.12 或 3.13 Kali或Win最好安装jdk1.8.5或之后版本 设置环境变量,如 ...

  10. POJ 2864

    #include <iostream> #define MAXN 600 using namespace std; int _m[MAXN][MAXN]; int main() { //f ...