PAT甲级1147 Heaps (30 分):[C++题解]堆、树的遍历、dfs、完全二叉树建树
生活随笔
收集整理的這篇文章主要介紹了
PAT甲级1147 Heaps (30 分):[C++题解]堆、树的遍历、dfs、完全二叉树建树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 題目分析
- 題目來源
題目分析
來源:acwing
分析:給定完全二叉樹,判斷是否是堆,需要區分大根堆,小根堆。后面是輸出后序遍歷。
AC代碼
#include<bits/stdc++.h> using namespace std;const int N = 1010; int h[N];int n, m; vector<int> res;void dfs(int u){if(2*u <= n) dfs(2*u);if(2*u+1 <= n) dfs(2*u+1);cout<<h[u];if( u != 1) cout<<" ";}int main(){cin >> m >> n;while(m--){for(int i =1; i<=n; i++) cin >> h[i];bool lt =false ,gt = false;for(int i = 1; i<= n; i++)for(int j = 0; j<2;j ++){if(i*2 +j <= n){int a = h[i], b= h[i*2 +j];if(a < b) lt = true;else gt = true;}}if( lt && gt) cout<<"Not Heap"<<endl;else if( lt) cout<<"Min Heap"<<endl;else cout <<"Max Heap"<<endl;dfs(1);cout<<endl;} }題目來源
PAT甲級1147 Heaps (30 分)
https://www.acwing.com/problem/content/1642/
總結
以上是生活随笔為你收集整理的PAT甲级1147 Heaps (30 分):[C++题解]堆、树的遍历、dfs、完全二叉树建树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级1140 Look-and-s
- 下一篇: PAT甲级1037 Magic Coup