当前位置: 首页 > news >正文

《算法笔记》9.2小节——数据结构专题(2)->二叉树的遍历 问题 A: 复原二叉树(同问题 C: 二叉树遍历)

题目描述

小明在做数据结构的作业,其中一题是给你一棵二叉树的前序遍历和中序遍历结果,要求你写出这棵二叉树的后序遍历结果。

输入

输入包含多组测试数据。每组输入包含两个字符串,分别表示二叉树的前序遍历和中序遍历结果。每个字符串由不重复的大写字母组成。

输出

对于每组输入,输出对应的二叉树的后续遍历结果。

样例输入
DBACEGF ABCDEFG
BCAD CBAD
样例输出
ACBFGED
CDAB

分析:不建树直接找的方法。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define ll long long
#define INF 0x3f3f3f3f
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,a) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<endl
using namespace std;
 
char preorder[1100],midorder[1100],lastorder[1100];
 
void getlastorder(char pre[],char mid[],int n)
{
    int t;
    if(n<=0)return;
    for(t=0;t<n;++t)
        if(mid[t]==pre[0])break;
    getlastorder(pre+1,mid,t);
    getlastorder(pre+t+1,mid+t+1,n-t-1);
    printf("%c",pre[0]);
}
 
int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
//    freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test
 
    while(~scanf("%s%s",preorder,midorder))
    {
        getlastorder(preorder,midorder,strlen(preorder));
        printf("\n");
    }
 
    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

建树再后序遍历的方法:由于前序遍历是先遍历根节点,因此前序遍历的第一个点一定是根节点。再到中序遍历中找到根节点的位置,这之前都是左子树,这之后都是右子树。知道左右子树长度之后,就可以在前序遍历中找到左右子树。这样递归地建立二叉树,最后输出后序遍历结果。

#include    <algorithm>
#include     <iostream>
#include      <cstdlib>
#include      <cstring>
#include       <string>
#include       <vector>
#include       <cstdio>
#include        <queue>
#include        <stack>
#include        <ctime>
#include        <cmath>
#include          <map>
#include          <set>
#include<unordered_map>
#define INF 0x3f3f3f3f
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,a) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<endl
#define db5(x,y,z,a,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<", "<<#r<<"="<<(r)<<endl
using namespace std;

typedef struct node
{
    char val;
    struct node *left,*right;
}node;

void Free(node *root)
{
    if(root->left!=NULL)Free(root->left);
    if(root->right!=NULL)Free(root->right);
    free(root);
    return;
}

node *get_tree(char *pre,int pre_l,int pre_r,char *mid,int mid_l,int mid_r)
{
    if(pre_l>=pre_r)return NULL;

    node *p=(node *)malloc(sizeof(node));
    p->val=pre[pre_l];

    int index=0;
    for(index=mid_l;index<mid_r;++index)
        if(mid[index]==pre[pre_l])
            break;

    p->left=get_tree(pre,pre_l+1,pre_l+1+index-mid_l,mid,mid_l,index);
    p->right=get_tree(pre,pre_l+1+index-mid_l,pre_r,mid,index+1,mid_r);

    return p;
}

void last_order(node *root)
{
    if(root==NULL)return;
    last_order(root->left);
    last_order(root->right);
    printf("%c",root->val);
    return;
}

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    char pre[100000],mid[100000];
    while(~scanf("%s%s",pre,mid))
    {
        int len=strlen(pre);
        node *root=get_tree(pre,0,len,mid,0,len);
        last_order(root);
        printf("\n");
        Free(root);
    }

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

相关文章:

  • 小程序电子画册制作,用户体验为王!
  • 【多线程】线程不安全问题
  • 每日学习Java之一万个为什么(待补充)
  • Web Component 教程(四):如何优雅的使用 template 模块
  • springboot集成xxl-job
  • 使用 libmodbus 实现 Modbus 通信
  • linux 出现网卡 down 没起来 怎么办 ? 已解决
  • C/C++编程:Openssl使用 Windows安装包32和64位 RSA加密/解密、AES-GCM加密/解密以及ECDSA签名/验证示例
  • C/C++蓝桥杯算法真题打卡(Day8)
  • 虚幻基础:组件组件通信
  • 一次http请求需要经过哪些步骤?
  • 【GPT入门】第26课 掌握langchain LCEL 链式调用的三种方法
  • Qt msvc程序运行
  • Vue3组合式函数(刷新率 useFps)
  • 搞定python之九----常用内置模块
  • linux环境下快速输出电脑的系统/硬件/显卡/网络/已安装软件等信息
  • AT指令集-NBIOT
  • 【Linux】深度解析Linux进程管理:从进程PCB到创建子进程的全景指南
  • 常见的前端安全问题
  • 探索HTML5 Canvas:创造动态与交互性网页内容的强大工具
  • “80后”蒋美华任辽宁阜新市副市长
  • 解放日报社论:只争朝夕、不负重托,加快建成具有全球影响力的科技创新高地
  • 王毅:携手做世界和平与发展事业的中流砥柱
  • 病人有头发,照护者不发疯:《黑镜》中的身体缺席与虚伪关怀
  • 在循环往复的拍摄中,重新发现世界
  • 巴防长称中俄可参与克什米尔恐袭事件国际调查,外交部回应