PAT2020春3月份 1155 Heap Paths (30 分) 经验分享和心路历程

题干:https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552

题解:50分钟满分解答,对看我博客的各位我真的很抱歉,虽然我写了经验分享和心路历程这样的语句,但是现在冲刺的时候真的很难再分心出来去总结这些,虽然我自己在现场做的时候有感受。。。我更想把这些时间用来自己休息一下或者去做别的题目。。。。真的很抱歉

// A1155.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int val;
    node* left;
    node* right;
};
int n;
vector<int> an(10010);
vector<vector<int>> path;
vector<int> temp_path;

node* bulid(int root) {
    if (root > n) {
        return NULL;
    }
    node* temp = new node();
    temp->val = an[root];
    temp->left = bulid(2 * root);
    temp->right = bulid(2 * root+1);
    return temp;
}

void dfs(node* root) {
    if (root->right == NULL && root->left == NULL) {
        path.push_back(temp_path);//叶子节点,左右都没有子树
        return;
    }
    if (root->right != NULL) {
        temp_path.push_back(root->right->val);
        dfs(root->right);
        temp_path.pop_back();
    }
    if (root->left != NULL) {
        temp_path.push_back(root->left->val);
        dfs(root->left);
        temp_path.pop_back();
    }
}

void dfs(int root) {
    if (2* root >n && 2* root +1>n) {
        path.push_back(temp_path);//叶子节点,左右都没有子树
        return;
    }
    if (2 * root + 1 <= n) {
        temp_path.push_back(an[2 * root + 1]);
        dfs(2 * root + 1);
        temp_path.pop_back();
    }
    if (2 * root <=n) {
        temp_path.push_back(an[2 * root]);
        dfs(2 * root);
        temp_path.pop_back();
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    FILE* S;
    freopen_s(&S, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE

    int t;
    cin >> n;
    for (int i = 1; i <=n; i++) {
        cin >> an[i];
        
    }
    //node* root = bulid(1);
    temp_path.push_back(an[1]);
    dfs(1);
    bool max_heap = true, min_heap = true;
    for (int i = 0; i < path.size(); i++) {
        cout << path[i][0];
        for (int j = 1; j < path[i].size(); j++) {
            cout << " " << path[i][j];
            if (path[i][j] >= path[i][j - 1])  max_heap = false;
            if (path[i][j] <= path[i][j - 1])  min_heap = false;
        }
        cout << endl;
    }
    if (max_heap) {
        cout << "Max Heap\n";
    }
    else if (min_heap) {
        cout << "Min Heap\n";
    }
    else {
        cout << "Not Heap\n";
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exodus&Focus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值