求助
查看原帖
求助
1287887
_zhaosihan_qwq_楼主2024/11/10 10:20
#include<bits/stdc++.h>
using namespace std;
struct str{
	int l,r;
}tree[1000005];
void x(int n){
	cout<<n<<' ';
	if(tree[n].l!=0)
		x(tree[n].l);
	if(tree[n].r!=0)
		x(tree[n].r);
}
void z(int n){
	if(tree[n].l!=0){
		z(tree[n].l);

	}
	cout<<n<<" ";
	if(tree[n].r!=0){
		z(tree[n].r);
	}
}
void h(int n){
	if(tree[n].l!=0){
		z(tree[n].l);

	}
	if(tree[n].r!=0){
		z(tree[n].r);
	}
	cout<<n<<" ";
}
int main(){
	int n;
	cin>>n;
	for(int i=1; i<=n; i++){
		cin>>tree[i].l>>tree[i].r;
	}
	x(1);
    cout<<endl;
    z(1);
    cout<<endl;
	h(1);
    cout<<endl;

	return 0;
}
  
2024/11/10 10:20
加载中...