问题 层序加中序确定树(代码有问题,答案在下面)(***********)

#include <cstdio>
#include <queue>
using namespace std;
struct node
{
    int data;
    node* lc;
    node* rc;
};
int n,pos[35],in[35];

node* creat(int cl,int cr,int il,int ir)
{
    int flag = 0;
    if(cl > cr || il > ir)
        return NULL;
    node* root = new node;
    int i,j;

    for(i = cl;i<=cr;i++)
    {
        for(j = il;j<=ir;j++)
        {
            if(in[j] == pos[i])
            {
                flag = 1;
                break;
            }
        }
        if(flag == 1)
            break;
    }
    root->data = in[j];
    int temp = root->data;
    root->lc = creat(cl+i,cr,il,j-1);
    root->rc = creat(cl+i,cr,j+1,ir);

    return root;
}

void proT(node*root)
{
    if(root == NULL)
        return;
    printf("%d ",root->data);
    proT(root->lc);
    proT(root->rc);
}
void levelT(node*root)
{
    int flag = 0;
    queue<node*>q;
    q.push(root);
    while(!q.empty())
    {
        node* s = q.front();
        q.pop();
        if(flag == 0)
        {
            printf("%d",s->data);
            flag = 1;
        }
        else
        {
            printf(" %d",s->data);
        }

        if(s->lc != NULL)
        {
            q.push(s->lc);
        }

        if(s->rc != NULL)
        {
            q.push(s->rc);
        }

    }
}

int main()
{

    freopen("in.txt","r",stdin);
    scanf("%d",&n);
   for(int i = 1;i<=n;i++)
   {
       scanf("%d",&pos[i]);
   }
   for(int i = 1;i<=n;i++)
   {
       scanf("%d",&in[i]);
   }
//   proT(creat(1,n,1,n));//4 1 3 2 6 5 7
   levelT(creat(1,n,1,n));


    return 0;
}
============

```。
#include <cstdio>
#include <queue>
using namespace std;

int ceng[100];
int zhon[100];


struct node
{
    int data;
    node* left;
    node* right;
 };

node* create(int cengl,int cengr,int zhonl,int zhonr)  //当我给你一个二叉树的中序遍历时 
{                                                    //这个中序遍历遍历一定有一个根节点 
    if(zhonl>zhonr)                                  //体现在层序遍历上时它一定先出现 
    {                                                //所以顺序遍历层序遍历,一定可以先发现二叉树的根节点 
        return NULL;      
    }
    node* root=new node;
    int i,j;
    int flag;
    for(i=0;i<=(cengr-cengl);i++)
    {
        flag=false;
        for(j=0;j<=(zhonr-zhonl);j++)
        {
            if(ceng[i+cengl]==zhon[j+zhonl])
            {
                flag=true;
                break;
            }
        }
        if(flag) break;
    }
    root->data=zhon[j+zhonl];                          
    root->left=create(cengl+i+1,cengr,zhonl,zhonl+j-1);
    root->right=create(cengl+i+1,cengr,zhonl+j+1,zhonr);  
    return root;
}

void cengxu(node* root)
{
    queue<node*> q;
    q.push(root);
    while(!q.empty())
    {
        node* top=q.front();
        q.pop();
        if(top!=root) printf(" ");
        printf("%d",top->data);
        if(top->left) q.push(top->left); 
        if(top->right) q.push(top->right);
    }
}


int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&ceng[i]);
    for(int i=0;i<n;i++) scanf("%d",&zhon[i]);
    node* root=create(0,n-1,0,n-1);
    cengxu(root);
    return 0;
}

发布了111 篇原创文章 · 获赞 4 · 访问量 3223

猜你喜欢

转载自blog.csdn.net/qq_15556537/article/details/99900195