RE求
查看原帖
RE求
1328844
72728abcd楼主2024/12/8 10:57

程序

#include<bits/stdc++.h>
using namespace std;
typedef struct node;
typedef node *tree;
int n;
int a[1000001],b[1000001];
tree c[1000001];
struct node
{
	int data;
	tree left,right;
};
void treecreate(tree t) 
{
	c[t->data]->left=c[a[t->data]];
	c[t->data]->right=c[b[t->data]];
	treecreate(c[a[t->data]]);
	treecreate(c[b[t->data]]);
}
void tral(tree t)
{
	if(t)
	{
		cout<<t->data<<" ";
		if(t->left->data)
		{
			tral(t->left);
		}
		if(t->right->data)
		{
			tral(t->right);
		}
	}
	cout<<endl;
	if(t)
	{
		if(t->left->data)
		{
			tral(t->left);
		}
		if(t->right->data)
		{
			tral(t->right);
		}
		cout<<t->data<<" ";
	}
	cout<<endl;
	if(t)
	{
		if(t->left->data)
		{
			tral(t->left);
		}
		if(t->right->data)
		{
			tral(t->right);
		}
		cout<<t->data<<" ";
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i];
	}
	for(int i=1;i<=n;i++)
	{
		c[i]->data=i;
	}
	treecreate(c[1]);
	tral(c[1]);
}
2024/12/8 10:57
加载中...