二叉树遍历A1020 A1086 A1102

本文探讨了三个关于二叉树遍历的题目A1020、A1086和A1102,特别指出在A1086中,使用(1,n,1,n)作为边界条件可能会导致输出错误,建议使用(0,n-1,0,n-1)以确保正确性。" 80039807,1373728,深度学习:神经网络与非线性假设,"['机器学习', '神经网络']

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

A1020

#include<bits/stdc++.h>
using namespace std;

struct node{
	int data;
	node* lchild;
	node* rchild;
};
int post[31],in[31];
int n;
//已知后序遍历和中序遍历创建二叉树 
node* create(int posl,int posr,int inl,int inr){
	if(posl>posr){
		return NULL;
	}
	node* root=new node;
	root->data=post[posr];
	int k;
	for(k=inl;k<=inr;k++){
		if(in[k]==post[posr]){
			break;
		}
	}
	int numleft=k-inl;//左子树的结点个数
	//左子树的后序区间为[posl,posl+numleft-1]中序区间为[inl,k-1] 
	//右子树的后序区间为[posl+numleft,posr-1]中序区间为[k+1,inr] 
	root->lchild=create(posl,posl+numleft-1,inl,k-1);
	root->rchild=create(posl+numleft,posr-1,k+1,inr);
	return root;
}
//层序遍历
int num=0;
void bfs(node* root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node* now=q.front();
		q.pop();
		printf("%d",now->data);
		num++;
		if(num<n) printf(" ");
		if(now->lchild!=NULL) q.push(now->lchild);
		if(now->rchild!=NULL) q.push(now->rchild);
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)//先输入后序遍历的结果 
	{
		scanf("%d",&post[i]);
	}
	for(int i=1;i<=n;i++)//再输入中序遍历的结果 
	{
		scanf("%d",&in[i]);
	}
	node* root=create(1,n,1,n);
	bfs(root);//层序遍历 
	return 0;
}

A1086

node* root=create(0,n-1,0,n-1);

这里注意,这里写成(1,n,1,n)输出错误,以后还是写成(0,n-1,0,n-1)

//已知先序序列和中序序列求后序序列 
#include<bits/stdc++.h>
using namespace std;

struct node{
	int data;
	node* lchild;
	node* rchild;
};
int n;
int pre[31],in[31],post[31];
//创建二叉树 
node* create(int prel,int prer,int inl,int inr)
{
	if(prel>prer)
	{
		return NULL;
	}
	node* root=new node;
	root->data=pre[prel];
	int k;
	for(k=inl;k<=inr;k++)
	{
		if(in[k]==pre[prel])
	    {
	    	break;
		 } 
	}
	int numleft=k-inl;
	root->lchild=create(prel+1,prel+numleft,inl,k-1);
	root->rchild=create(prel+numleft+1,prer,k+1,inr);
	return root;//返回根结点地址 
}
//求后序序列
int num=0;
void postorder(node* root)
{
	if(root==NULL)
	{
		return;
	}
	postorder(root->lchild);
	postorder(root->rchild);
	printf("%d",root->data);
	num++;
	if(num<n) printf(" ");
 } 
int main()
{
	char str[5];
	scanf("%d",&n);
	stack<int> st;
	int x,preindex=0,inindex=0;
	for(int i=0;i<2*n;i++)
	{
		scanf("%s",str);
		if(strcmp(str,"Push")==0)//入栈 
		{
			scanf("%d",&x);
			pre[preindex++]=x;
			st.push(x);
		}
		else
		{
			in[inindex++]=st.top();
			st.pop();
		}
	}
	node* root=create(0,n-1,0,n-1);
	postorder(root);
	return 0;
 } 

A1102

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
struct node{
	int lchild,rchild;
}Node[maxn];
bool notroot[maxn]={false};
int n,num=0;//n为结点个数,num为当前已经输出的结点个数 

void printf(int id){
	printf("%d",id);
	num++;
	if(num<n) printf(" ");
	else printf("\n");
}
//中序遍历输出 
void inorder(int root){
	if(root==-1){
		return;
	}
	inorder(Node[root].lchild);
	printf(root);
	inorder(Node[root].rchild);
} 
//层序遍历输出
void bfs(int root){
	queue<int> q;
	q.push(root);
	while(!q.empty()){
		int now=q.front();
		q.pop();
		printf(now);
		if(Node[now].lchild!=-1) q.push(Node[now].lchild);
		if(Node[now].rchild!=-1) q.push(Node[now].rchild);
	}
} 
//后序遍历,用以反转二叉树
void postorder(int root){
	if(root==-1){
		return;
	}
	postorder(Node[root].lchild);
	postorder(Node[root].rchild);
	swap(Node[root].lchild,Node[root].rchild);//交换左右孩子结点 
} 
//将输入的字符转换为-1或结点编号 
int strtonum(char c){
	if(c=='-') return -1;
	else{
		notroot[c-'0']=true;
		return c-'0';
	}
}

int findroot(){
	for(int i=0;i<n;i++){
		if(notroot[i]==false){
			return i;
		}
	} 
}
 
int main()
{
	char lchild,rchild; 
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%*c%c %c",&lchild,&rchild);
		Node[i].lchild=strtonum(lchild);
		Node[i].rchild=strtonum(rchild);
	}
	int root=findroot();
	postorder(root);
	bfs(root);
	num=0;
	inorder(root);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值