天梯赛平时练习(关于二叉树)

由序列遍历构建二叉树的模板

#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
const long long MAXN = 1e5 + 7;
typedef long long ll;

struct node
{
	int data;
	node* lchild;
	node* rchild;
};
int pre[MAXN]; //前序
int in[MAXN]; //中序
int bhd[MAXN]; //后序

由前中序遍历生成二叉树的模板

//前中序生成树
node *create_VLR_LVR(int preL, int preR, int inL, int inR)
{
	if(preL>preR)
		return NULL;

	node *root=new node;
	root->data=pre[preL];
	int i;
	for(i=inL;i<=inR;i++){
		if(in[i]==pre[preL])
			break;
	}

	int nodeLeft=i-inL;
	root->lchild=create_VLR_LVR(preL+1,preL+nodeLeft,inL,i-1);
	root->rchild=create_VLR_LVR(preL+nodeLeft+1,preR,i+1,inR);
	return root;
}

由后序和中序的排序生成二叉树

//后中序生成树
node *create_LRV_LVR(int bhdL, int bhdR, int inL, int inR)
{

	if(bhdL>bhdR||bhdL<0||bhdR<0)
		return NULL;
	node *root=new node;
	root->data=bhd[bhdR];

	int i;
	for(i =inL;i<=inR;i++){
		if(in[i]==bhd[bhdR])
			break;
	}
	int numRight=inR-i;
	root->lchild=create_LRV_LVR(bhdL,bhdR-(numRight+1),inL,i-1);
	root->rchild=create_LRV_LVR(bhdR-numRight,bhdR-1,i+1,inR);
	return root;
}

前序遍历输出二叉树

//前序输出
void printpre(node* root)
{
    cout << root->data << " ";
    if(root->lchild!=NULL)
        printpre(root->lchild);
    if(root->rchild!=NULL)
        printpre(root->rchild);
}

中序遍历输出二叉树

//中序输出
void printin(node* root)
{

    if(root->lchild!=NULL)
        printin(root->lchild);
    cout << root->data << " ";
    if(root->rchild!=NULL)
        printin(root->rchild);
}

后序遍历输出二叉树

//后序输出
void printbhd(node* root)
{

    if(root->lchild!=NULL)
        printbhd(root->lchild);
    if(root->rchild!=NULL)
        printbhd(root->rchild);
    cout << root->data << " ";
}

层序输出遍历二叉树

//层序输出
int flag=0;
void printcengxu(node *bt)
{
	queue<node*> q;
	node *temp;
	if(bt==NULL) return;
	q.push(bt);
	while(!q.empty()){
		temp=q.front();
		q.pop();
		if(flag)
		cout<<" ";
		cout<<temp->data;
		flag=1;

		if(temp->lchild!=NULL) q.push(temp->lchild);
		if(temp->rchild!=NULL) q.push(temp->rchild);

	}

7-28 树的遍历 (25分)
题解:
套用两个模板
后中遍历生成树+层序遍历输出树

#include<bits/stdc++.h>
#define ll long long
#define speed_up ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const long long MAXN = 1e5 + 7;

struct node
{
	int data;
	node* lchild;
	node* rchild;
};
int in[MAXN]; //中序
int bhd[MAXN]; //后序

//后中序生成树
node *create_LRV_LVR(int bhdL, int bhdR, int inL, int inR)
{

	if(bhdL>bhdR||bhdL<0||bhdR<0)
		return NULL;
	node *root=new node;
	//通过后序遍历找到每一个子树的头结点(左子树和右子树)
	root->data=bhd[bhdR];

	int i;
	//通过中序推断出左子树的长度
	for(i =inL;i<=inR;i++){
		if(in[i]==bhd[bhdR])
			break;
	}
	//i 是左子树的长度
	//numRight-1 是右子树的长度
	int numRight=inR-i;
	//设置左指针的时候
	//下一步再找左子树的头结点
	//当这个左子树全部遍历完成之后才会返回一开始就定下的那个头结点
	root->lchild=create_LRV_LVR(bhdL,bhdR-(numRight+1),inL,i-1);
	                          //后序遍历的数组中减掉右子树的长度,从下标为0处开始
	                          //中序遍历的数组传入时只从头开始传到左子树的长度
	root->rchild=create_LRV_LVR(bhdR-numRight,bhdR-1,i+1,inR);
	                          //后序遍历的数组
	                          //尾部减掉右子树的长度到尾(最后一个数)减一
	                          //相当于传入了一个右子树
	                          //中序遍历
	                          //从找到那个根节点处开始一直到后面都是右子树
	return root;
	//所有递归都结束之后才会最后返回一个值
	//是最头的头结点
}
//层序遍历函数
void leveorder(node* root)
{
    node* Q[MAXN],*q=NULL;
    int front=-1,rear=-1;
    if(root==NULL)
    {
        cout<<endl;
        return;
    }
    Q[++rear]=root;
    int falg=0;
    while(front!=rear)
    {
        q=Q[++front];
        if(falg==0)
        {
            cout<<q->data;
            falg++;
        }
        else
        {
            cout<<" "<<q->data;
            falg++;
        }
        if(q->lchild!=NULL)
        {
            Q[++rear]=q->lchild;
        }
        if(q->rchild!=NULL)
        {
            Q[++rear]=q->rchild;
        }
    }
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>bhd[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>in[i];
    }
    node* root=create_LRV_LVR(0, n-1, 0, n-1);
    leveorder(root);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值