Trees on the level UVA - 122 (二叉树的层次遍历)
题目链接:https://vjudge.net/problem/UVA-122
题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值。每个结点都按照从根节点到它的移动序列给出(L表示左,R表示右) 在输入中,每个结点的左括号
和右括号之间没有空格,相邻结点之间用一个空格隔开。每棵树的输入用一对空括号)()结束 入上图所示:
注意:如果从根结点到某个叶节点的路径有的结点没有在输入中给出,或者给出超过一次,输出not complete。 结点个数不超过256
思路:显然是二叉树的层次遍历。
首先看一下输入部分:
bool read_input()//读入字符
{
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;
}
这里要学一下sscanf的用法 strchr的用法
接下来看一下建树的代码:
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;//标记已经赋值
}
建完树之后便是层次遍历的过程了,这里用bfs来写:
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 remove_tree(Node* u)
{
if(u==NULL) return ;
remove_tree(u->Left);//递归释放左子树的空间
remove_tree(u->Right);//
delete u;
}
下面看完整代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=+;
char s[maxn];//保存读入的节点
bool failed;
struct Node{
bool have_value;//是否被赋值过
int v;//结点值
Node *Left,*Right;
Node()
{
have_value=false;
Left=NULL;
Right=NULL;
}
};
Node *root;
void remove_tree(Node* u)
{
if(u==NULL) return ;
remove_tree(u->Left);//递归释放左子树的空间
remove_tree(u->Right);//
delete u;
}
Node* newnode()
{
return new Node();
}
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()//读入字符
{
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;
}
/*
这样一来 输入和建树部分就已经结束了 接下来只需要按照层次顺序遍历这棵树
此处使用一个队列来完成这个任务 初始时只有一个根节点 然后每次取出一个节点
就把它的左右子结点放入队列中
*/
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()
{ while(read_input())
{
vector<int>ans;
vector<int>::iterator it;
if(failed||(!bfs(ans))) printf("not complete\n");
else
{
it=ans.begin();
printf("%d",*it);
it++;
for(it;it!=ans.end();it++)
printf(" %d",*it);
printf("\n");
} } return ;
}
Trees on the level UVA - 122 (二叉树的层次遍历)的更多相关文章
- UVa 122 (二叉树的层次遍历) Trees on the level
题意: 输入一颗二叉树,按照(左右左右, 节点的值)的格式.然后从上到下从左到右依次输出各个节点的值,如果一个节点没有赋值或者多次赋值,则输出“not complete” 一.指针方式实现二叉树 首先 ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- LeetCode: 103_Binary Tree Zigzag Level Order Traversal | 二叉树Zigzag层次遍历 | Medium
本题也属于层次遍历的变形,不同之处在于其遍历的方法是交替进行的,形成一个ZigZag的曲线形式,如下: 代码如下: struct TreeNode { int val; TreeNode* left; ...
- 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)
这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...
- 102 Binary Tree Level Order Traversal 二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 ...
- UVa 122 树的层次遍历
题意: 给定一颗树, 按层次遍历输出. 分析: 用数组模拟二叉树, bfs即可实现层次遍历 #include <bits/stdc++.h> using namespace std; st ...
- leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历
基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...
随机推荐
- Entity Framework Tutorial Basics(7):DBContext
DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...
- TinkerPop中的遍历:图的遍历步骤(1/3)
图遍历步骤(Graph Traversal Steps) 在最一般的层次上,Traversal<S,E>实现了Iterator,S代表起点,E代表结束.遍历由四个主要组成部分组成: Ste ...
- Java50道经典习题-程序49 子串出现的个数
题目:计算首末不含空格各个子串之间只含一个空格的字符串中子串出现的次数分析:例如输入的字符串为"I come from County DingYuan Province AnHui.&quo ...
- jQuery之$.support.xxx
下面这段代码来自jQuery-file-upload 9.19官方Demo $(function () { 'use strict'; // Change this to the location o ...
- Java连接Hbase异常
Exception in thread "main" org.apache.hadoop.hbase.client.RetriesExhaustedException: Faile ...
- ubuntu - 14.04,安装、配置GO语言开发工具Eclipse!!
在配置Eclipse之前,我们必须保证下面这些都已经安装,并且正常工作了: 一,Go语言:参考文章 http://blog.csdn.net/sunylat/article/details/49859 ...
- 编译原理-First集和Follow集
刚学first集和follow集的时候,如果上课老师没有讲明白或者自己没听明白,自己看的时候还真是有点难理解,不过结合着具体的题目可以理解的更快. 先看一下两种集合的求法: First集合的求法: ...
- 容器编排之Kubernetes1.7.6安装与配置
kubernetes官网的安装教程是采用kubeadm init的方式,但是在生产环境当中,可能需要独自手动安装k8s,本文采用源码安装的方式,一步步搭建k8s的master节点和node节点. 系统 ...
- 8.Move Zeroes(移动零)
Level: Easy 题目描述: Given an array nums, write a function to move all 0's to the end of it while mai ...
- [Maven]How do I tell Maven to use the latest version of a dependency?
Link: http://stackoverflow.com/questions/30571/how-do-i-tell-maven-to-use-the-latest-version-of-a-de ...