查看原帖
1098756
wzx2012楼主2025/1/15 17:48

自认为是树建错了,但没找到问题,求

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
int a[maxn], b[maxn];
struct node{
	int val;
	int l, r;
} tree[maxn];
void front(node t){
	if(t.val == 0)	return;
	cout << t.val << ' ';
	front(tree[t.l]);
	front(tree[t.r]);
}
void middle(node t){
	if(t.val == 0)	return;
	front(tree[t.l]);
	cout << t.val << ' ';
	front(tree[t.r]);
}
void back(node t){
	if(t.val == 0)	return;
	front(tree[t.l]);
	front(tree[t.r]);
	cout << t.val << ' ';
}
int main(){
	int n;	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> a[i] >> b[i];
		tree[i].val = i;
		tree[i].l = a[i];
		tree[i].r = b[i];
	}
	front(tree[1]);cout << endl;
	middle(tree[1]);cout << endl;
	back(tree[1]);cout << endl;
	return 0;
}
2025/1/15 17:48
加载中...