uva-122 Trees on the level(树的遍历)
题目:
给出一棵树的表示,判断这棵树是否输入正确,如果正确就按层次遍历输出所有的结点,错误的话就输出not complete。
思路:
根据字符串中树的路径先将树建起来,在增加结点和层次遍历树的时候判断这棵树是不是输入正确。
错误的两种情况:
1.同一个结点被输入的两次
2.这个结点的孩子有值,但这个结点没有被输入值。
判断方法:
根据字符串中给出的路径,将整棵树先建起来,每个结点中设一个是否被访问过的标志vis
增加结点时如果这个结点的vis已经为true则为错误的情况1。
遍历树的时候,如果这个结点已经建起来了,但是value没有被赋值,则为错误情况2。
最后利用队列对树进行层次遍历,并输出。
代码:
C++
- #include <bits/stdc++.h>
- #define inf 0x3f3f3f3f
- #define MAX 1e9;
- #define FRE() freopen("in.txt","r",stdin)
- #define FRO() freopen("out.txt","w",stdout)
- using namespace std;
- typedef long long ll;
- const int maxn = ;
- struct Node{
- Node* rt;
- Node* lt;
- int value;
- bool vis;
- Node(){
- rt = lt = NULL;
- value = ;
- vis = false;
- }
- };
- bool fail = false;
- Node* root = new Node();
- void removeTree(Node* u){//释放树占的内存空间
- if(u==NULL) {return;}
- removeTree(u->lt);
- removeTree(u->rt);
- delete u;
- }
- int makeNum(string str){//提取字符串中的数字
- int ans = ;
- for(int i = ; str[i]!=','; i++){
- ans = ans* + str[i]-'';
- }
- return ans;
- }
- void addNode(Node* root,int value,string str){
- Node* p = root;
- for(int i = ; i<str.length(); i++){//忽略字符串中不是L和R的字符
- if(str[i]=='L'){//L则向左建树
- if(p->lt==NULL){
- p->lt = new Node();
- }
- p = p->lt;
- }else if(str[i]=='R'){//R则向右建树
- if(p->rt==NULL){
- p->rt = new Node();
- }
- p = p->rt;
- }
- }
- if(p->vis){//如果这个结点已经被赋值,则是错误输入情况1
- fail = true;
- }
- p->value = value;//赋值
- p->vis = true;//标记已经访问过
- }
- void printTree(Node* root){
- vector<int> ans;
- queue<Node*> que;
- que.push(root);
- while(!que.empty()){
- Node* temp = que.front();
- que.pop();
- if(!temp->vis) fail = true;//如果这个结点被建出来,但是没有被赋值,则是错误情况2
- ans.push_back(temp->value);
- if(temp->lt) que.push(temp->lt);
- if(temp->rt) que.push(temp->rt);
- }
- if(fail){
- cout<<"not complete"<<endl;
- }else{
- cout<<ans[];
- for(int i = ; i<ans.size(); i++){
- cout<<" "<<ans[i];
- }
- cout<<endl;
- }
- }
- int main(){
- string str;
- while(cin>>str){
- if(str=="()"){//这棵树输入结束,打印树
- printTree(root);
- removeTree(root);
- root = new Node();
- fail = false;
- }else{
- int value = makeNum(str);
- //cout<<str<<" "<<value<<endl;
- addNode(root, value, str);
- }
- }
- return ;
- }
Java
- import java.io.*;
- import java.util.*;
- class Node{
- Node rt;
- Node lt;
- int value;
- boolean vis;
- public Node() {
- rt = null;lt = null;
- value = 0;vis = false;
- }
- }
- public class Main {
- public static boolean fail = false;
- public static void printTree(Node root) {
- Queue<Node> que = new LinkedList<>();
- LinkedList<Integer> list = new LinkedList<>();
- if(root==null) {
- fail = true;
- }else {
- que.offer(root);
- while(!que.isEmpty()) {
- Node temp = que.peek();que.poll();
- if(!temp.vis) {fail = true;}
- list.add(temp.value);
- if(temp.lt!=null)
- que.offer(temp.lt);
- if(temp.rt!=null)
- que.offer(temp.rt);
- }
- }
- if(!fail) {
- System.out.print(list.get(0));
- for(int i = 1; i<list.size(); i++) {
- System.out.print(" "+list.get(i));
- }
- System.out.println();
- }else {
- System.out.println("not complete");
- }
- }
- public static void addNode(Node root, int value, String str) {
- Node p = root;
- for(int i = 0; i<str.length(); i++) {
- if(str.charAt(i)=='L') {
- if(p.lt==null) {
- p.lt = new Node();
- }
- p = p.lt;
- }else if(str.charAt(i)=='R') {
- if(p.rt==null) {
- p.rt = new Node();
- }
- p = p.rt;
- }
- }
- if(p.vis) {
- fail = true;
- }
- p.value = value;
- p.vis = true;
- }
- public static int makeNum(String str) {
- int num = 0;
- for(int i = 1; str.charAt(i)!=','; i++) {
- num = num*10 + str.charAt(i)-'0';
- }
- return num;
- }
- public static void main(String[] args) {
- Node root = new Node();
- Scanner scan = new Scanner(System.in);
- String str=null;
- while(scan.hasNext()) {
- str = scan.next();
- if(str.equals("()")) {
- printTree(root);
- root = new Node();
- fail = false;
- }else {
- int value = makeNum(str);
- addNode(root, value, str);
- }
- }
- scan.close();
- }
- }
用java做这个题的时候,利用split方法来处理字符串,结果一直是RE,找不出来了,就换了中方法(如上述代码中的)来实现,AC。
uva-122 Trees on the level(树的遍历)的更多相关文章
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- 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 ...
- UVa 122 Trees on the level
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
随机推荐
- 在linux上处理base64加密和解密
http://snailwarrior.blog.51cto.com/680306/142472/ 2.从标准输入读取文件内容,base64编码并打印到标准输出 [root@localhost tes ...
- window环境下在anconda中安装opencv
今日学习CNN神经网络,在用keras框架下搭建一个简单的模型的时候需要import cv2,我尝试了一下几种方法: 1. 在prompt输入 pip intall opencv-python 出现如 ...
- 3-1 todolist功能开发
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Tomcat启动Web.xml引用其它XML配置报FileNotFound异常解决方案
如果使用JEECG框架进行Tomcat启动时,如果web.xml引用了其他xml文件,需要在tomcat文件夹里的config文件夹里的context.xml文件里的Context标签里配置xmlBl ...
- bzoj题目大体分类
http://m.blog.csdn.net/article/details?id=51387623
- ngCordova插件说明
转载自 http://my.oschina.net/u/1416844/blog/495026 参 考http://blog.csdn.net/superjunjin/article/details/ ...
- robotframework - Edit编辑器
1.测试项目&套件 提供的Edit编辑器 2.在 Edit 标签页中主要分:加载外部文件.定义内部变量.定义元数据等三个部分. (1):加载外部文件Add Library:加载测试库,主要是[ ...
- 1051 复数乘法(C#)
一.题目内容如下: 复数可以写成 ( 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 1:也可以写成极坐标下的指数形式 (,其中 R 是复数模,P 是辐角,i 是虚数单位,其等价于三角 ...
- java entity
对java实体类的众多理解: A .就是属性类,通常定义在model层里面 B. 一般的实体类对应一个数据表,其中的属性对应数据表中的字段.好处:1.对对象实体的封装,体现OO思想.2.属性可以对字段 ...
- ACM_数数有多少(第二类Stirling数-递推dp)
数数有多少 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小财最近新开了一家公司,招了n个员工,但是因为资金问题,办公楼只有m间 ...