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;
}
上一篇:2021秋季《数据结构》_ EOJ 1096.Building Roads


下一篇:EOJ_1015_查字典