拼多多笔试第二题

昨天没通过,今天发现多大了一个空格,不知道现在能不能通过全部案例。

#include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

struct node
{
    string name;
    vector<node *> son;
    node *parent;
};

bool sortCmp(node* const a1,  node* const a2)
{
    return a1->name < a2->name;
}

void printTree(node* n, string str)
{
    for(int i=0; i<n->son.size(); i++)
    {
        string strTemp = str;
        cout<<str;
        if(i < n->son.size()-1)
            cout<<"|-- "<<n->son[i]->name<<endl;
        else
            cout<<"`-- "<<n->son[i]->name<<endl;

        if(i<n->son.size()-1)
            strTemp += "|   ";
        else
            strTemp += "    ";

        if(n->son[i]->son.size()>0)
        {
            printTree(n->son[i], strTemp);
        }
    }
}

int main()
{
    int N;
    cin>>N;
    vector<node> data(N);
    node *head = NULL;
    for(int i=0; i<N; i++)
    {
        int id;
        cin>>data[i].name>>id;
        if(id != -1)
            data[id].son.push_back(&data[i]);
    }
    for(int i=0; i<N; i++)
        sort(data[i].son.begin(), data[i].son.end(), sortCmp);

    cout<<data[0].name<<endl;
    printTree(&data[0], "");
}
全部评论

相关推荐

04-06 11:24
已编辑
太原学院 C++
点赞 评论 收藏
分享
求面试求offer啊啊啊啊:1600一个月?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务