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 ...
随机推荐
- iView页面Modal中内嵌Tabs,重新显示Modal时默认选中Tabs的第一项
文档中说激活面板的name用value,页面第一次加载的时候可以,放在modal里就不好使了,每次打开的时候总显示上一次离开时的界面. 真正能用的是 this.$refs.tabs.activeKey ...
- <一>企业级开源仓库nexus实战应用–nexus的安装
1,Nexus 介绍. Nexus是什么? Nexus 是一个强大的maven仓库管理器,它极大地简化了本地内部仓库的维护和外部仓库的访问. 不仅如此,他还可以用来创建yum.pypi.npm.doc ...
- <三>企业级开源仓库nexus3实战应用–使用nexus3配置maven私有仓库
一两个星期之前,你如果在我跟前说起私服的事情,我大概会绕着你走,因为我对这个东西真的一窍不通.事实上也正如此,开发同学曾不止一次的跟我说公司的私服版本太旧了,许多新的依赖编译之后不会从远程仓库自动缓存 ...
- 分布式监控系统开发【day38】:报警阈值程序逻辑解析(三)
一.需求讨论 1.请问如何解决延迟问题 1000台机器,每1分钟循环一次但是刚好第一次循环第一秒刚处理完了,结果还没等到第二分钟又出问题,你那必须等到第二次循环,假如我这个服务很重要必须实时知道,每次 ...
- Mybatis-PageHelper
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- 第七节:利用CancellationTokenSource实现任务取消和利用CancellationToken类检测取消异常。
一. 传统的线程取消 所谓的线程取消,就是线程正在执行的过程中取消线程任务. 传统的线程取消,是通过一个变量来控制,但是这种方式,在release模式下,被优化从cpu高速缓存中读取,而不是从内存中读 ...
- 使用ZooKeeper协调多台Web Server的定时任务处理(方案1)
背景说明: 有一套Web服务程序, 为了保证HA, 需要在多台服务器上部署, 该服务程序有一些定时任务要执行, 现在要保证的是, 同一定时任务不会在多台机器上被同时执行. 方案1 --- 任务级的主备 ...
- python学习01
1.python怎样运行? 1)打开运行窗口,输入python 2)打开运行窗口,直接输入 python + python代码 `python -c "print(1/6.878)&quo ...
- 【codeforces 914H】Ember and Storm's Tree Game
原题链接 Description Ember和Storm正在玩游戏.首先,Ember构造一棵n个节点且每个节点度数不超过d的带节点编号的树T.然后,Storm选择两个不同的节点u和v,并写下从u到v路 ...
- (一)校园信息通微信小程序从前端到后台整和笔记
前段时间接触了微信小程序,现在回过头来做一些笔记. 先上效果图 后台数据管理界面(PHP) 校园信息通微信小程序前端界面 下面先简单的说一下怎样部署一个微信小程序 首先是前端 微信小程序有它专门的开发 ...