L2-006 树的遍历(25 分) +L2-011 玩转二叉树 (25 分)

版权声明:个人笔记,仅供复习 https://blog.csdn.net/weixin_42373330/article/details/88827363

描述:

L2-006 树的遍历(25 分)
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:
在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
4 1 6 3 5 7 2


参考:https://blog.csdn.net/weixin_42373330/article/details/81909534
思路在参考里
这里用c[i]数组存储树,(i<<1)表示当前位的左儿子,(i<<1|1)表示右儿子
这题N最大为30,按我的方法数组就有2^30大小,明显不行,但是却过了,可能数据不够全面
代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const long int N = 100500;
int c[N];
int mx=0;
void dfs(int n,int a[],int b[],int cnt)
{
    if(n<=0) return ;
    mx=max(mx,cnt);
    int mid;
    int p=a[n-1];
    c[cnt]=p;
    for(int i=0;i<n;i++)
        if(p==b[i]) {mid=i;break;}


    dfs(mid,a,b,(cnt*2));//左

    int e[1000];
    int d[1000];
    for(int i=mid;i<n-1;i++)
        {d[i-mid]=a[i];}

    for(int i=mid+1;i<n;i++)
       {
           e[i-mid-1]=b[i];

       }
    dfs(n-mid-1,d,e,((cnt*2)+1));//右



}
int main()
{
    int a[1000];//后序
    int b[1000];//中序
    memset(c,0,sizeof(c));
    int n;
    cin>>n;
    if(n==0)
        return 0;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=0;i<n;i++)
        cin>>b[i];
    int cnt = 1;
    dfs(n,a,b,cnt);
    int ans=1;
    for(int i=1;i<=mx;i++)
        if(c[i])
        {

            if(ans<n)
            {
                cout<<c[i]<<" ";
                ans++;
            }
            else if(ans==n)
           {
               cout<<c[i]<<endl;
               ans++;
               break;
           }
           else
                break;

        }
    return 0;
}

这里有一篇是用结构体指针实现的==链接==


L2-011 玩转二叉树 (25 分)
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。

输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。

输出格式:
在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
输出样例:
4 6 1 7 5 3 2

代码:

这篇代码是参考上题用结构体指针建树的链接写的

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const long int N = 100;
struct node
{
    int data;
    node *lson;
    node *rson;
};
int pre[N],mid[N],last[N];
node *creat(int lpre,int rpre,int lmid,int rmid)
{
    if(lpre>rpre)
        return NULL;
    node * root = new node;
    root->data=pre[lpre];
    int id;
    for(id=lmid;id<=rmid;id++) if(root->data ==mid[id])
        break;
    int len=id-lmid;//左儿子的长度
    root->lson= creat(lpre+1,lpre+1+len-1,lmid,id-1);
    root->rson = creat(lpre+len+1,rpre,id+1,rmid);
    return root;
}
void exchangeL_to_R(node *root)
{
    if(root==NULL)
        return ;
    node *p;
    p=root->lson;
    root->lson=root->rson;
    root->rson=p;
    exchangeL_to_R(root->lson);
    exchangeL_to_R(root->rson);
    return;
}

int n;

void bfs(node *root)
{
    queue<node*> q;
    node *t;
    q.push(root);
    int cnt=0;
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(cnt<n-1)
            cout<<t->data<<" ";
        else
            cout<<t->data<<endl;
        cnt++;
        if(t->lson!=NULL) q.push(t->lson);
        if(t->rson!=NULL) q.push(t->rson);
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>mid[i];
    for(int i=0;i<n;i++)
        cin>>pre[i];
    node *root = creat(0,n-1,0,n-1);
    exchangeL_to_R(root);
    bfs(root);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42373330/article/details/88827363