88的雷达10901923012039

yang_zhiyu 2024-01-24 20:59:51

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

struct Node
{
	int lc,rc;	
};

const int N=100050;
Node tree[N];
vector<int> kuan[N];
int maxdep;

void preorder(int cur,int dep) 
{
	if (cur == 0) return ;
	dep++;
	maxdep=max(maxdep,dep);
	kuan[dep].push_back(cur);
	preorder(tree[cur].lc,dep);
	preorder(tree[cur].rc,dep);
}

int main()
{
	int n;
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		cin>>tree[i].lc>>tree[i].rc;
	}
	
	preorder(1,0);
	
	for(int i=1;i<=maxdep;i++)
	{
		cout<<kuan[i][kuan[i].size()-1]<<endl;
	} 
	
	return 0;
	return 0;
}

共 2 条回复

jacky
jacky