1155 Heap Paths (30 分)

1147 Heaps (30 分)类似,\(30\)分水了。

const int N=1010;
int heap[N];
bool minheap,maxheap;
int n;

void dfs(int u,vector<int> &path)
{
    if(u*2 > n)//叶子结点
    {
        for(int i=0;i<path.size();i++)
        {
            if(i)
            {
                if(path[i] > path[i-1]) maxheap=false;
                else if(path[i] < path[i-1]) minheap=false;
                cout<<' '<<path[i];
            }
            else cout<<path[i];
        }
        cout<<endl;
        return;
    }

    if(u*2+1 <= n)
    {
        path.pb(heap[u*2+1]);
        dfs(u*2+1,path);
        path.pop_back();
    }
    if(u*2 <= n)
    {
        path.pb(heap[u*2]);
        dfs(u*2,path);
        path.pop_back();
    }
}

int main()
{
    cin>>n;

    for(int i=1;i<=n;i++) cin>>heap[i];

    minheap=maxheap=true;
    vector<int> path;
    path.pb(heap[1]);

    dfs(1,path);

    if(maxheap) puts("Max Heap");
    else if(minheap) puts("Min Heap");
    else puts("Not Heap");

    //system("pause");
    return 0;
}
posted @ 2021-03-11 22:28  Dazzling!  阅读(32)  评论(0编辑  收藏  举报