题目链接:

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. centos7安装mantis

    安装mantis的步骤如下: 1. 安装apache, mysql等必要软件 1 #yum update 2 #yum install httpd php php-pdo php-mysql php- ...

  2. 关于DFS和BFS的理解 以及坐标的定义

    http://blog.csdn.net/bool_isprime/article/details/5803018DFS: 1: 坐标类型搜索 :这种类型的搜索题目通常来说简单的比较简单,复杂的通常在 ...

  3. python 使用json格式转换

    什么是json: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programm ...

  4. HttpClient的帮助类

    /// <summary> /// http请求类 /// </summary> public class HttpHelper { private HttpClient _h ...

  5. [leetcode.com]算法题目 - Pow(x, n)

    Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...

  6. CF1082解题报告

    CF1082A Vasya and Book 模拟一下即可 \(Code\ Below:\) #include <bits/stdc++.h> using namespace std; c ...

  7. jdk在Linux下的安装

    之前学linux的时候,也是赶鸭子上架,照着教程一步一步的走,但是今天linux出现了点问题重装一下 重新学习一些linux下常需软件的安装 一般我们再装完系统后,装的匆忙,先把ip搞出来 切换到该目 ...

  8. nginx root&alias 文件路径配置

    nginx 指定文件路径有两种方式 root 和 alias,root 与 alias 主要区别在于 nginx 如何解释 location 后面的 uri,这会使两者分别以不同的方式将请求映射到服务 ...

  9. ArrayList的源码分析

    在项目中经常会用到list集合来存储数据,而其中ArrayList是用的最多的的一个集合,这篇博文主要简单介绍ArrayList的源码分析,基于JDK1.7: 这里主要介绍 集合 的属性,构造器,和方 ...

  10. 为什么要先 git add 才能 git commit

    1. git 的 add ,是一个容易引起疑问的命令.在 subversion 中的 svn add 动作是将某个文件加入版本控制,而 git add的意义完全不同. 同时, git diff --c ...