感想:
水题,中序遍历
#include<iostream>
#include<vector>
#include<map>
#include<deque>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct tree{
string s ;
tree* left;
tree* right;
tree* father;
}t[30];
tree* root;
void inorder(tree* a){
if(a->right&&a!=root)
cout<<"(";
if(a->left)
inorder(a->left);
cout<<a->s;
if(a->right)
inorder(a->right);
if(a->right&&a!=root)
cout<<")";
}
int main(){
int N,i,k,j;
string s1;
cin>>N;
for(i=1;i<=N;i++){
cin>>s1>>j>>k;
t[i].s=s1;
if(j!=-1){
t[i].left=&t[j];
t[j].father=&t[i];
}
if(k!=-1){
t[i].right=&t[k];
t[k].father=&t[i];
}
}
root=&t[1];
while(root->father){
root=root->father;
}
inorder(root);
}