题意:

给出先序和中序,求后序。

思路:

①建树然后递归输出。

 //建树的
#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 建树+不建树的更多相关文章

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

  2. UVa 536 Tree Recovery(二叉树后序遍历)

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...

  3. UVa 536 Tree Recovery

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...

  4. UVA - 536 Tree Recovery (二叉树重建)

    题意:已知先序中序,输出后序. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio& ...

  5. 【UVA】536 Tree Recovery(树型结构基础)

    题目 题目     分析 莫名A了     代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void buil ...

  6. UVa 699 The Falling Leaves(递归建树)

    UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每 ...

  7. UVA 536 (13.08.17)

     Tree Recovery  Little Valentine liked playing with binary trees very much. Her favoritegame was con ...

  8. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  9. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

随机推荐

  1. BSGS及扩展BSGS算法及例题

    \(BSGS(baby-step-giant-step)\)算法是用来解高次同余方程的最小非负整数解的算法,即形如这个的方程: \(a^x\equiv b(mod\ p)\) 其中\(p\)为质数(其 ...

  2. Springboot 5.Springboot 返回cookies信息的post接口开发

    首先创建一个类,类里面首先登陆获取到cookie,然后带着cookie去发送请求 package com.course.server; import com.course.bean.User; imp ...

  3. A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem

    文章名称:A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem 发表时间:2017 期 ...

  4. RSA Encrypting/Decrypting、RSA+AES Encrypting/Decrypting

    catalogue . CryptoAPI介绍 . RSA Encrypting/Decrypting File 1. CryptoAPI介绍 0x1: Cryptography Service Pr ...

  5. SpringCloud笔记五:Feign

    目录 什么是Feign? 有了Ribbon我还要Feign干嘛? 新建consumer-feign 修改api项目 引入Maven文件 新建feign的接口 启动项目 报错 发泄发泄心情,一个段落 什 ...

  6. 一个select元素自定义设计的新思路:appearance: none之后利用<>符号制造小箭头

    最近工作时解决了一个前端小问题(如下图所示):在Safari中,select的控件之上有不和谐的灰色部分. 刚开始时我以为是backgrand或是border设置不当之类产生的问题,在搜索了很久之后终 ...

  7. [物理学与PDEs]第2章习题2 质量力有势时的能量方程

    试证明: 如果质量力有势, 即存在 $\phi$ 使 ${\bf F}=-\n \phi$, 那么理想流体的能量守恒方程的微分形式可写为 $$\bex \cfrac{\rd}{\rd t}\sex{e ...

  8. JS实现定时任务,每隔N秒请求后台——setInterval定时和ajax请求

    DiGui = function (param) { $.ajax({ success: function (returnValue) { window.setInterval("fnSet ...

  9. 选择性搜索(SS)算法

    一.目标检测和目标识别 目标识别(object recognition)是要指明一张图像中包含哪类目标.输入是图像,输出是图像中的目标属于的类别(class probability).目标检测是识别出 ...

  10. 计算int数组中的最大,最小,平均值

    public static void testNumber(int[] arr) { int max = arr[0]; int min = arr[0]; int avg = 0; int sum ...