EOJ_1064_树的层号表示法
作者:互联网
#include <bits/stdc++.h>
using namespace std;
struct node
{
int lev;
char data;
node* parent;
vector<node*> sons;
};
node arr[30];
node* buildTree(node arr[], int n)
//返回树根节点
{
node* tree = &arr[0];
node* p = tree;
for(int i=1;i<n;i++){
node* tmp = &arr[i];
while(p->lev >= tmp->lev) p = p->parent;
p->sons.push_back(tmp);
tmp->parent = p;
p = tmp;
}
return tree;
}
void postOrder(node* tree)
{
for(int i=0 ; i<tree->sons.size() ; i++){
postOrder(tree->sons[i]);
}
cout << tree->data;
}
int main()
{
int n;
cin>>n;
char tmp;
for(int i=0;i<n;i++){
cin >> tmp >> arr[i].lev >> tmp >> arr[i].data >> tmp;
arr[i].parent = NULL;
tmp = getchar();
}
node* tree;
tree = buildTree(arr, n);
postOrder(tree);
return 0;
}
标签:tmp,node,arr,parent,int,EOJ,tree,1064,表示法 来源: https://blog.csdn.net/xyc859666356/article/details/121161589