天梯赛 树的遍历 玩转二叉树 (前中求后 中后求前 层次遍历 镜面反转)

           天梯赛 树的遍历 玩转二叉树 (前中求后  中后求前 层次遍历 镜面反转)

     寒假集训学二叉树的时候,当时的状态就是当时学当时会,第二天就忘了,二叉树内容比较多,当时也没学会,尤其是已知前序 中序求后序和已知中序 后序求前序还有层次遍历一点也不会,当时也就放下了,后来也没有用到,一直也没学(懒),知道第一次天梯赛的模拟赛,遇见一个中后求前+层次遍历,当时看了一眼就知道不会,放下了,那时心想以后不会出了吧,结果天梯赛初赛的时候,又有一个前中求后+镜面反转+层次遍历,唉,还是不会,回来,才开始学了, 遇见不会的要立马去学,千万别拖。废话不多说,上题。
    1.树的遍历:点击打开链接

   
    
     
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;
struct node
{
    int data;
    struct node *l,*r;
};
int ct=0;
struct node *cz(int z[],int q[],int l)
{
    if(l==0)
    {
        return NULL;
    }
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node ));
    p->data=q[l-1];
    int i=0;
    for(;i<l;i++)
    {
        if(z[i]==q[l-1])
        {
            break;
        }
    }
    p->l=cz(z,q,i);
    p->r=cz(z+i+1,q+i,l-(i+1));

    return p;

}
void printf(struct node *p,int n)
{
    queue<node *>h;
    struct node *x;
    while(!h.empty())
    {
        h.pop();
    }
    if(p)
    {
        h.push(p);
    }
    while(!h.empty())
    {
        x=h.front();
        h.pop();
        ct++;
        printf("%d",x->data);
        if(ct!=n)
        {
            printf(" ");
        }
        if(x->l)
        {
            h.push(x->l);
        }
        if(x->r)
        {
            h.push(x->r);
        }
    }
}
int main()
{
    int n,m,i,j;
    int a[100],b[100];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
    }
    struct node *k;
    k=cz(b,a,n);
    printf(k,n);



    return 0;
}
    

      2.玩转二叉树:点击打开链接



   镜面反转就是先输出右子树,再输出左子树

  
#include <bits/stdc++.h>
using namespace std;
int ct=0;
struct node
{
    int data;
    struct node *l,*r;
};
struct node *cz(int z[],int q[],int l)
{
    if(l==0)
    {
        return NULL;
    }
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node ));
    p->data=q[0];
    int i=0;
    for(;i<l;i++)
    {
        if(z[i]==q[0])
        {
            break;
        }
    }
    p->l=cz(z,q+1,i);
    p->r=cz(z+i+1,q+i+1,l-(i+1));

    return p;

}
void printf(struct node *p,int n)
{
    queue<node *>q;
    struct node *x;
    if(p)
    {
        q.push(p);
    }
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        ct++;
        printf("%d",x->data);
        if(ct!=n)
        {
            printf(" ");
        }
        if(x->r)
        {
            q.push(x->r);
        }
        if(x->l)
        {
            q.push(x->l);
        }

    }
}
int main()
{
    int n,i;
    int z[100],q[100];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&z[i]);
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&q[i]);
    }
    struct node *k;
    k=cz(z,q,n);
    printf(k,n);



    return 0;
}

          多复习!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值