又是一个20分,不知道那5分错在哪
第一次完全自己写树,还是很欣慰的
复习memset(a,0,sizeof(a))
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <cstring>
using namespace std;
int n;
struct node{
int left,right;
};
int arr[11];
int tree[100];
vector<node> cnt;
vector<int> ans;
void over(int k){
if(tree[k]==-1) return ;
over(k*2+1);
ans.push_back(tree[k]);
over(k*2+2);
}
int main(){
cin >> n;
cnt.resize(n);
memset(tree,-1,sizeof(tree));
for(int i=0; i<n; i++){
char a,b;
int aa,bb;
cin >> a >> b;
if(a<='9'&&a>='0') aa = (int)(a - '0');
else aa = -1;
if(b<='9'&&b>='0') bb = (int)(b - '0');
else bb = -1;
cnt[i].left = aa;
cnt[i].right = bb;
arr[aa] = 1;
arr[bb] = 1;
}
for(int i=0; i<n; i++){
if(arr[i]==0){
tree[0] = i;
}
}
for(int i=0; i<n; i++){
if(cnt[tree[i]].right!=-1&&tree[i]!=-1) tree[i*2+1] = cnt[tree[i]].right;
if(cnt[tree[i]].left!=-1&&tree[i]!=-1) tree[i*2+2] = cnt[tree[i]].left;
}
for(int i=0; i<100; i++){
if(tree[i]!=-1){
if(i==0) cout << tree[i] ;
else cout << " " << tree[i];
}
}
cout << endl;
over(0);
for(int i=0; i<n; i++){
if(i==0) cout << ans[i] ;
else cout << " " << ans[i];
}
return 0;
}