二叉树的遍历

前序,中序-->后序
#include<stdio.h>
#include<string.h>
int a[1010],b[1010];
int flag;
void  build(int x1,int y1,int x2,int y2)
{
    if(x1>y1)
        return ;
    int x=a[x1];
    int p=x2;
    while(b[p]!=x) p++;
    int cnt=p-x2; //左子树的个数
    build(x1+1,x1+cnt,x2,p-1);
    build(x1+cnt+1,y1,p+1,y2);
    if(flag==0)
    {
        printf("%d",x);
        flag=1;
    }
    else
        printf(" %d",x);
    return ;
}
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        flag=0;
        for(int i=0; i<t; i++)
            scanf("%d",&a[i]); //前序
        for(int j=0; j<t; j++)
            scanf("%d",&b[j]); //中序
        build(0,t-1,0,t-1);
        printf("\n");
    }
    return 0;
}



中序,后序-->前序

#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define MAX 1010
int a[MAX];//中序
int b[MAX];//后序
void build(int x1,int y1,int x2,int y2)
{
    if(x2>y2)
        return;
    int x=b[y2];
    printf("%d ",x);//输出前序
    int p=x1;
    while(a[p]!=x)
        p++;
    int cnt=p-x1;
    build(x1,p-1,x2,x2+cnt-1);
    build(p+1,y1,x2+cnt,y2-1);
    return ;
}
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        for(int i=0; i<t; i++)
            scanf("%d",&a[i]);  //中序
        for(int j=0; j<t; j++)
            scanf("%d",&b[j]);  //后序
        build(0,t-1,0,t-1);
        printf("\n");
    }
    return 0;
}


中序,前序-->层序

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define MAX 100
int a[MAX],b[MAX];
struct Node
{
    int lch;
    int rch;
} s[MAX];
int build(int x1,int y1,int x2,int y2)//中序,前序
{
    if(x2>y2)
        return 0;
    int x=b[x2];
    int p=x1;
    while(a[p]!=x)
        p++;
    int cnt=p-x1;
    s[x].lch=build(x1,p-1,x2+1,x2+cnt);
    s[x].rch=build(p+1,y1,x2+cnt+1,y2);
    return x;
}
void print(int x)
{
    queue<int>Q;
    Q.push(x);
    int flag=0;
    while(!Q.empty())
    {
        int t=Q.front();
        Q.pop();
        printf("%d ",t);//输出层序遍历
        if(s[t].lch!=0)
            Q.push(s[t].lch);
        if(s[t].rch!=0)
            Q.push(s[t].rch);
    }
    printf("\n");
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);//中
    for(int j=0; j<n; j++)
        scanf("%d",&b[j]);//前
    build(0,n-1,0,n-1);
    print(b[0]);//根
    return 0;
}

中序,后序-->层序

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define MAX 100
int a[MAX],b[MAX];
struct Node
{
    int lch;
    int rch;
} s[MAX];
int build(int x1,int y1,int x2,int y2)//中序,后序
{
    if(x2>y2)
        return 0;
    int x=b[y2];
    int p=x1;
    while(a[p]!=x)
        p++;
    int cnt=p-x1;
    s[x].lch=build(x1,p-1,x2,x2+cnt-1);
    s[x].rch=build(p+1,y1,x2+cnt,y2-1);
    return x;
}
void print(int x)
{
    queue<int>Q;
    Q.push(x);
    int flag=0;
    while(!Q.empty())
    {
        int t=Q.front();
        Q.pop();
        printf("%d ",t);
        if(s[t].lch!=0)
            Q.push(s[t].lch);
        if(s[t].rch!=0)
            Q.push(s[t].rch);
    }
    printf("\n");
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);//中
    for(int j=0; j<n; j++)
        scanf("%d",&b[j]);//后
    build(0,n-1,0,n-1);
    print(b[n-1]);//根
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值