题目描述:

题目思路:

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)的更多相关文章

  1. Uva 122 树的层次遍历 Trees on the level lrj白书 p149

    是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担 ...

  2. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

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

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  4. UVa 122 (二叉树的层次遍历) Trees on the level

    题意: 输入一颗二叉树,按照(左右左右, 节点的值)的格式.然后从上到下从左到右依次输出各个节点的值,如果一个节点没有赋值或者多次赋值,则输出“not complete” 一.指针方式实现二叉树 首先 ...

  5. Trees on the level UVA - 122

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateo ...

  6. 【紫书】Trees on the level UVA - 122 动态建树及bfs

    题意:给你一些字符串,代表某个值被插入树中的位置.让你输出层序遍历. 题解:动态建树. 由于输入复杂,将输入封装成read_input.注意输入函数返回的情况 再将申请新节点封装成newnode(). ...

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

  8. [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 ...

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

    [抄题]: 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) [思维问题]: 不知道反复切换要怎么做:用boolean normalOrder当作布尔型 ...

随机推荐

  1. TortoiseSVN 分支创建与合并

    前提准备: 确保本地Work Copy 和 服务器上的 版本一致.( 所有代码都提交到SVN,并update一次) 1  从主干创建分支代码 在本地Work Copy  选中项目文件夹,鼠标右键选择 ...

  2. day01 三级菜单

    知识点: 字典, for 循环,if ...else 嵌套 永辉超市 = { '食品饮料':{ '休闲食品':{ '坚果':['山核桃','腰果'], '炒货':['瓜子','花生'] }, '牛奶' ...

  3. oracle 数据库密码生产同步模拟环境 ,随记常用命令

    1.查看当前open用户 select username,account_status,expiry_date,profile from dba_users; 2.查看目前的密码过期策略 select ...

  4. Windows10:Opencv4.0+Opencv4.0.1_contrib编译

    操作系统:windows10 64bit 已安装工具:VS2017 64bit,cmake3.12bit. 安装Cmake:到cmake下载3.12及以上版本,64bit, 选择windows下的安装 ...

  5. CSS实现图片等比例缩小不变形

    <img src="../images/bg1.jpg" alt="" /> img { /*等宽缩小不变形*/ /*width: 100%;*/ ...

  6. windows10上安装mysql

    环境:windwos 10(1511) 64bit.mysql 5.7.14 一.下载mysql 1. 在浏览器里打开mysql的官网http://www.mysql.com/ 2. 进入页面顶部的& ...

  7. react-router-dom和本地服务本地开发 (node、webpack)

    场景 使用react 做开发,避免会使用react-router React Router 已经是V4的版本 React Router 目前已经被划分成了三个包:react-router,react- ...

  8. Java并发之线程状态及Thread常用方法

    本篇文章主要讲解线程的虚拟机状态和线程基本方法,希望可以加深对线程的使用理解. 一.线程的虚拟机状态 线程对象在不同的运行期间有不同的状态,状态信息定义在Thread公共静态枚举java.lang.T ...

  9. 新人成长之入门Vue.js弹窗Dialog介绍(二)

    前言 在上一篇博文中介绍了Vue.js的常用指令,今天总结归纳一下弹窗Dialog的使用,弹窗经常被使用在一些表单的增删改查啊,或者弹出一些提示信息等等,在保留当前页面状态的情况下,告知用户并承载相关 ...

  10. DQL数据查询

    set hive.fetch.task.conversion=more; -- 避免触发MR job select distinct name from employee_id limit 2; -- ...