Note
- 二叉树
- 一开始没有读懂题…(太菜了orz)
- input:编号(第几行)代表根节点与左右孩子连接
- 反转二叉树:我直接把左孩子存为右孩子,右孩子存为左孩子…
- 明确给出结点编号之间的关系–>用"静态二叉树"
Code
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> order(2);
struct node{
int data,left,right;
}nod[15];
void inorder(int root){
if(root==-1) return;
inorder(nod[root].left);
order[1].push_back(nod[root].data);
inorder(nod[root].right);
}
void level(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int no=q.front();
q.pop();
order[0].push_back(nod[no].data);
if(nod[no].left!=-1) q.push(nod[no].left);
if(nod[no].right!=-1) q.push(nod[no].right);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int n;
char l,r;
cin>>n;
getchar();
int root;
bool hash[n];
memset(hash,false,sizeof(hash));
for(int i=0;i<n;i++){
nod[i].data=i;
scanf("%c %c",&l,&r);
getchar();
if(l=='-') nod[i].right=-1;
else{
nod[i].right=l-'0';
hash[l-'0']=true;
}
if(r=='-') nod[i].left=-1;
else{
nod[i].left=r-'0';
hash[r-'0']=true;
}
}
for(int i=0;i<n;i++){
if(hash[i]==false){
root=i;
break;
}
}
inorder(root);
level(root);
for(int i=0;i<2;i++){
for(int j=0;j<order[i].size();j++){
if(j!=0) printf(" ");
printf("%d",order[i][j]);
}
printf("\n");
}
return 0;
}