已知树的中序+后序/先序遍历,建立二叉树

本文介绍如何根据二叉树的中序和后序遍历或中序和先序遍历来构建二叉树,并提供了详细的C++实现代码。通过递归方式找到根节点并划分左右子树,最终构建完整的二叉树结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

已知树的中序和后序遍历,建立二叉树(个人感觉理解不了可以直接硬记,慢慢就能理解,知道思想不能理解也无所谓,会用就行)


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n;
int hou[50];
int zhong[50];
typedef struct Btree
{
    int data;
    struct Btree* left;
    struct Btree* right;
};
Btree* build(int *hou,int *zhong,int len)
{
    if(len<=0) return NULL;
    Btree* tmp=new Btree;
    tmp->data=hou[len-1];
    int num=0;
    for(int i=0;i<len;i++)
    {
        if(zhong[i]==hou[len-1])
        {
            num=i;
            break;
        }
    }
    int index=num;
    tmp->left=build(hou,zhong,index);
    tmp->right=build(hou+index,zhong+index+1,len-index-1);
    return tmp;
}
int ans[500];
int cur;

void print(Btree *root)
{
    queue<Btree*> Q;
    Q.push(root);
    Btree *tmp=Q.front();
    cout<<tmp->data;
    if(tmp->left!=NULL) Q.push(tmp->left);
    if(tmp->right!=NULL) Q.push(tmp->right);
    Q.pop();
    while(!Q.empty())
    {

        Btree *tmp=Q.front();
        Q.pop();
        cout<<" "<<tmp->data;
        //ans[cur++]=tmp->data;
        if(tmp->left!=NULL)
        {
           // cout<<tmp->data;
            Q.push(tmp->left);
        }
        if(tmp->right!=NULL)
        {

            Q.push(tmp->right);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&hou[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&zhong[i]);
    }
    Btree *root=build(hou,zhong,n);
    //print(root);
    return 0;
}

已知树的中序和先序遍历,建立二叉树

*#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n;
int xian[50];
int zhong[50];
typedef struct Btree
{
    int data;
    struct Btree* left;
    struct Btree* right;
};
//typedef struct Btree Btree;
Btree* build(int *xian,int *zhong,int len)
{
    if(len<=0) return NULL;
    Btree* tmp=new Btree;
    tmp->data=xian[0];
    int num=0;
    for(int i=0;i<len;i++)
    {
        if(zhong[i]==xian[0])
        {
            num=i;
            break;
        }
    }
    int index=num;
    tmp->left=build(xian+1,zhong,index);
    tmp->right=build(xian+index+1,zhong+index+1,len-index-1);
    return tmp;
}
void print(Btree *root)
{
    queue<Btree*> Q;
    Q.push(root);
    Btree *tmp=Q.front();
    cout<<tmp->data;
    if(tmp->right!=NULL) Q.push(tmp->right);
    if(tmp->left!=NULL) Q.push(tmp->left);

    Q.pop();
    while(!Q.empty())
    {

        Btree *tmp=Q.front();
        Q.pop();
        cout<<" "<<tmp->data;
        //ans[cur++]=tmp->data;
        if(tmp->right!=NULL)
        {

            Q.push(tmp->right);
        }
        if(tmp->left!=NULL)
        {
           // cout<<tmp->data;
            Q.push(tmp->left);
        }
    }
}

int main()
{
    scanf("%d",&n);

    for(int i=0;i<n;i++)
    {
        scanf("%d",&zhong[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&xian[i]);
    }
    Btree *root=build(xian,zhong,n);
    //print(root);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值