二叉树 1、给定先序遍历 2、给定先序和中序 3、给定后续和中序 4给定先序和后序

该博客介绍了如何通过先序遍历、中序遍历和后序遍历构建二叉树,并提供了C++代码实现。同时,针对给定的后序遍历和中序遍历,展示了求解层次遍历的方法。这些算法对于理解和操作二叉树数据结构至关重要。

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

1.二叉树

1.1给定先序遍历建立二叉树
原题链接

#include<iostream>
using namespace std;

int position;
char ss[105];

struct TreeNode {
	char cc;
	TreeNode * leftNode;
	TreeNode * rightNode;
	
	
	TreeNode(char c):cc(c),leftNode(NULL),rightNode(NULL){
	}
	
};

TreeNode * build()
{
	char x=ss[position++];
	if(x=='#')
		return NULL;
	
	TreeNode * root=new TreeNode(x);
	
	root->leftNode=build();
	root->rightNode=build();
	
	return root;
	
}

void InOrder(TreeNode * root)
{
	if(!root)
		return;
	
	InOrder(root->leftNode);
	printf("%c ",root->cc);
	InOrder(root->rightNode);
}

int main()
{
	scanf("%s",ss);
	TreeNode * root=build();
	InOrder(root);	
	return 0;
 } 

1.2给定一棵二叉树的前序遍历和中序遍历,求其后序遍历
原题链接

#include<iostream>
using namespace std;
#include<string>

struct TreeNode {
	char cc;
	TreeNode * leftNode;
	TreeNode * rightNode;
	
	TreeNode(char x):cc(x),leftNode(NULL),rightNode(NULL){
	}
};

TreeNode * build(string str1,string str2)
{
	if(!str1.size())
		return NULL;
	
	char xx=str1[0];
	
	int loc=str2.find(xx);
	
	TreeNode * root=new TreeNode(xx);
	
	root->leftNode=build(str1.substr(1,loc+1-1),str2.substr(0,loc));
	root->rightNode=build(str1.substr(loc+1),str2.substr(loc+1));
	return root; 
}

void PosOrder(TreeNode * root)
{
	if(root==NULL)
		return ;
	
	PosOrder(root->leftNode);
	PosOrder(root->rightNode);
	printf("%c",root->cc);
}

int main()
{
	string str1;
	string str2;
	
	while(getline(cin,str1))
	{
		getline(cin,str2);
		
		TreeNode * root=build(str1,str2);
		PosOrder(root);
		printf("\n");
	}
	
	
	return 0;
 } 

1.3、给定二叉树的后序遍历和中序遍历,请你输出它的层序遍历
原题链接

#include<iostream>
using namespace std;
#include<queue>
#include<string>

const int N=50;

int a[N];
int b[N];

struct TreeNode {
	int cc;
	TreeNode * leftNode;
	TreeNode * rightNode;
	
	TreeNode(char x):cc(x),leftNode(NULL),rightNode(NULL){
	}
};

queue<TreeNode *> qq;
int top;

TreeNode * build(int beg1,int end1,int beg2,int end2)
{
	if(beg1>end1)
		return NULL;
	
	int xx=a[end1];
	
	int loc;
	
	for(int i=beg2;i<=end2;i++)
	{
	    if(b[i]==xx)
	    {
	        loc=i;
	        break;
	    }
	}
	
	TreeNode * root=new TreeNode(xx);
	
	root->leftNode=build(beg1,loc-beg2-1+beg1,beg2,loc-1);
	root->rightNode=build(loc-beg2-1+beg1+1,end1-1,loc+1,end2);
	return root; 
}

void LevOrder(TreeNode * root)
{
    if(!root)
        return;
    
    qq.push(root);
    
    while(!qq.empty())
    {
        TreeNode * tt=qq.front();
        qq.pop();
        printf("%d ",tt->cc);
        
        if(tt->leftNode)
            qq.push(tt->leftNode);
        if(tt->rightNode)
            qq.push(tt->rightNode);
    }
    
    
}

int main()
{
    int n;
	string str1="";
	string str2="";
	
    cin>>n;
    
    for(int i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
    }

		
	TreeNode * root=build(0,n-1,0,n-1);
	LevOrder(root);

	printf("\n");

	
	
	return 0;
 } 

1.4、给定先序遍历和后序遍历,且是满二叉树,输出层次遍历

题目类似1.3

#include<iostream>
using namespace std;
#include<queue>
#include<string>

const int N=50;

int a[N];
int b[N];

struct TreeNode {
	int cc;
	TreeNode * leftNode;
	TreeNode * rightNode;
	
	TreeNode(char x):cc(x),leftNode(NULL),rightNode(NULL){
	}
};

queue<TreeNode *> qq;
int top;

TreeNode * build(int beg1,int end1,int beg2,int end2)
{
	if(beg1==end1)
	{
	    int xx=a[end1];
	    TreeNode * root=new TreeNode(xx);
	    root->leftNode=NULL;
	    root->rightNode=NULL;
	    return root;
	}

	int xx=a[beg1];
	int yy=a[beg1+1];
	
	int loc;
	
	for(int i=beg2;i<=end2;i++)
	{
	    if(b[i]==yy)
	    {
	        loc=i;
	        break;
	    }
	}
	
	TreeNode * root=new TreeNode(xx);
	
	root->leftNode=build(beg1+1,loc-beg2+beg1+1,beg2,loc);
	root->rightNode=build(loc-beg2+beg1+2,end1,loc+1,end2-1);
	return root; 
}

void LevOrder(TreeNode * root)
{
    if(!root)
        return;
    
    qq.push(root);
    
    while(!qq.empty())
    {
        TreeNode * tt=qq.front();
        qq.pop();
        printf("%d ",tt->cc);
        
        if(tt->leftNode)
            qq.push(tt->leftNode);
        if(tt->rightNode)
            qq.push(tt->rightNode);
    }
    
    
}

int main()
{
    int n;
	
    cin>>n;
    
    for(int i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
    }

		
	TreeNode * root=build(0,n-1,0,n-1);
	LevOrder(root);

	printf("\n");

	
	
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值