UVA 536 Tree Recovery 建树+不建树
题意:
给出先序和中序,求后序。
思路:
①建树然后递归输出。
//建树的
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; int n;
char pre[],mid[];
int judge[];
struct node{ char c;
node *left,*right;
node()
{
c='a';
left=right=NULL;
}
};
int cnt=,counter;
node* build(node *root)
{
char c=pre[cnt++];//从a中取数 去b中查找
int i;
for( i=;i<n;i++)
{
if(mid[i]==c)
break;
}
judge[i]=;
root=new node();
root->c=c;
if(i>&&i<n&&judge[i-]!=)//在b数组中,如果一个数左相邻的数被标记,则不能向左建树
root->left=build(root->left);
if(i>=&&i<n-&&judge[i+]!=)//同样,在b数组中,如果一个数右相邻的数被标记,则不能向右建树
root->right=build(root->right);
return root; //左右都建完,返回根结点
} void postorder(node *root)
{
if(root->left)
postorder(root->left);
if(root->right)
postorder(root->right);
if(counter)
//printf(" ");
//counter++;
printf("%c",root->c);
} int main()
{
while(scanf("%s %s",pre,mid)==)
{
//printf("%s %s\n",pre,mid);
counter = ;
cnt = ;
n=strlen(pre);
memset(judge,,sizeof(judge)); node *root = NULL;
root = build(root);
postorder(root);
printf("\n");
}
return ;
}
②不建树直接输出。
//不建树
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std; int n;
char pre[],mid[];
int cnt=,counter;
void work(int left,int right)//左右端点
{
int i;
char c=pre[cnt++];
for(i=left;i<right;i++)
{
if(mid[i]==c)
break;
}
if(i>left&&i<right)
work(left,i);
if(i>=left&&i<right-)
work(i+,right);
printf("%c",c);
} int main()
{
while(scanf("%s %s",pre,mid)==)
{
//printf("%s %s\n",pre,mid);
counter = ;
cnt = ;
n=strlen(pre); work(,n);
printf("\n");
}
return ;
}
UVA 536 Tree Recovery 建树+不建树的更多相关文章
- UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)
传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- UVa 536 Tree Recovery
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...
- UVA - 536 Tree Recovery (二叉树重建)
题意:已知先序中序,输出后序. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio& ...
- 【UVA】536 Tree Recovery(树型结构基础)
题目 题目 分析 莫名A了 代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void buil ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- UVA 536 (13.08.17)
Tree Recovery Little Valentine liked playing with binary trees very much. Her favoritegame was con ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
随机推荐
- day08读取文件
可参考;https://www.cnblogs.com/gengcx/p/6713646.html主要内容: 1.只读 2.只写 3.追加 4.r+读写 5.w+写读 6.a+写读 7.其他一.使用p ...
- elastalert 配置post告警方式(备忘)
最近在做把elk告警日志发送到kinesis 流,供后续数据分析处理使用........ 基于尽量不修改elastalert ,把修改工作放到接收端服务的原则.计划把elk的告警数据通过远程api ...
- 3537. 【NOIP2013提高组day2】华容道(搜索 + 剪枝)
Problem 给出一个类似华容道的图.\(q\)次询问,每次给你起始点,终止点,空格位置,让你求最少步数 \(n,m\le 30, q\le 500\). Soultion 一道智障搜索题. 弱智想 ...
- elasticsearch简单实现
初次接触分布式是全文搜索引擎,之前都是spinx+coreseek,先简单实现初步了解先 官方文档:https://www.elastic.co/guide/cn/elasticsearch/guid ...
- Java反射、反射练习整理
反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语 ...
- Deepin linux 远程桌面无法被Ubuntu连接
linux下远程桌面的工具还是有很多的,这个方法主要是解决Ubuntu自带的 Remmina无法远程 Deepin 桌面. 1.安装vncserver的基础服务,输入以下命令: sudo apt-ge ...
- JavaScript 日期和时间基础知识
前言 学习Date对象之前,首先要先了解关于日期和时间的一些知识.比如,闰年.UTC等等.深入了解这些,有助于更好地理解javascript中的Date对象. 标准时间 一般而言的标准时间是指GMT和 ...
- Best Practice API
# 建议直接使用的第三方类 Common Lang =>StringUtils =>Validate Guava =>Cache =>Ordering JDK7(LTS JDK ...
- CemtOS7更改yum网易 阿里云的yum源。
一,鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源,改为国内的Yum源,著名的有网易 阿里云源.如何更改呢? 二,更改yum源为网易的. 首先备份/etc/yum.repos.d/Cent ...
- js高级知识---词法分析和AO 链
转载自https://www.cnblogs.com/OceanHeaven/p/4957704.html 上面一篇文章说了js的作用域链,这一节算是对上面的延申,有一个典型的例子,首先看原来的一段代 ...