PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

题目

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_ (data_structure)) One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.
Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.
Output Specification:
For each given tree, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its lef subtree.
Finally print in a line Max Heap if it is a max heap, or Min Heap for a min heap, or Not Heap if it is not a heap at all.
Sample Input 1:
8
98 72 86 60 65 12 23 50
Sample Output 1:
98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap
Sample Input 2:
8
8 38 25 58 52 82 70 60
Sample Output 2:
8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap
Sample Input 3:
8
10 28 15 12 34 9 8 56
Sample Output 3:
10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap

题目分析

已知完全二叉树层序序列,打印所有路径(从根节点到叶子节点)并判断是否为堆,为大顶堆还是小顶堆

解题思路

  1. 打印路径
    思路01:dfs深度优先遍历,用整型数组path[n]记录路径进行回溯
    思路02:dfs深度优先遍历,用vector vin链表记录路径进行回溯
  2. 判断是否为堆,为大顶堆还是小顶堆
    思路01:递归判断,用父节点与其左右子节点进行比较判断
    思路02:for循环,用所有子节点与其父节点进行比较判断

Code

Code 01

#include <iostream>
using namespace std;
/*
	利用数组回溯 
*/
int level[1001],path[1001];
int n;
void printPath(int pin) {
	for(int i=0; i<=pin; i++) {
		printf("%d",path[i]);
		printf("%s",i==pin?"
":" ");
	}
}
void dfs(int vin, int pin) {
	path[pin]=level[vin];
	if(2*vin+1>=n) { //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
		printPath(pin);
		return;
	} else if(2*vin+2>=n) { //左子节点非NULL 右子节点为NULL 
		path[pin+1]=level[2*vin+1]; //添加左子节点后 打印退出 
		printPath(pin+1);
		return;
	} else {
		dfs(2*vin+2,pin+1);
		dfs(2*vin+1,pin+1);
	}
}
bool isMaxHeap(int vin) {
	if(2*vin+1>=n)return true; //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
	if(2*vin+1<n&&level[2*vin+1]>level[vin])return false;
	if(2*vin+2<n&&level[2*vin+2]>level[vin])return false;
	return isMaxHeap(2*vin+1)&&isMaxHeap(2*vin+2);
}
bool isMinHeap(int vin) {
	if(2*vin+1>=n)return true; //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
	if(2*vin+1<n&&level[2*vin+1]<level[vin])return false;
	if(2*vin+2<n&&level[2*vin+2]<level[vin])return false;
	return isMinHeap(2*vin+1)&&isMinHeap(2*vin+2);
}
int main(int argc,char * argv[]) {
	scanf("%d",&n);
	for(int i=0; i<n; i++) {
		scanf("%d",&level[i]);
	}
	dfs(0,0);
	if(isMaxHeap(0)) {
		printf("Max Heap
");
	} else if(isMinHeap(0)) {
		printf("Min Heap
");
	} else {
		printf("Not Heap
");
	}
	return 0;
}

Code 02

#include <iostream>
#include <vector>
using namespace std;
/*
	利用链表回溯
*/
int level[1001];
vector<int> path;
int n,isMax=1,isMin=1;
void printPath() {
	for(int i=0; i<path.size(); i++) {
		printf("%d",path[i]);
		printf("%s",i==pin?"
":" ");
	}
}
void dfs(int vin) {
	if(2*vin+1>=n) { //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
		printPath(); 
	} else if(2*vin+2>=n) {//左子节点非NULL 右子节点为NULL
		path.push_back(level[2*vin+1]); 
		printPath();
		path.pop_back();
	} else {
		path.push_back(level[2*vin+2]); 
		dfs(2*vin+2);
		path.pop_back();
		path.push_back(level[2*vin+1]); 
		dfs(2*vin+1);
		path.pop_back();
	}
}
int main(int argc,char * argv[]) {
	scanf("%d",&n);
	for(int i=0; i<n; i++) {
		scanf("%d",&level[i]);
	}
	path.push_back(level[0]); 
	dfs(0);
	for(int i=1;i<n;i++){
		if(level[(i-1)/2]>level[i])isMin=0; //如果i是从1存储的,这里应该是level[i/2]>level[i] 
		if(level[(i-1)/2]<level[i])isMax=0;
	}
	if(isMax==1) {
		printf("Max Heap
");
	} else if(isMin==1) {
		printf("Min Heap
");
	} else {
		printf("Not Heap
");
	}
	return 0;
}

Code 03

#include <iostream>
using namespace std;
/*
	利用数组回溯 
*/
int level[1001],path[1001];
int n,isMax=1,isMin=1;
void printPath(int pin) {
	for(int i=0; i<=pin; i++) {
		printf("%d",path[i]);
		printf("%s",i==pin?"
":" ");
	}
}
void dfs(int vin, int pin) {
	path[pin]=level[vin];
	if(2*vin+1>=n) { //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
		printPath(pin);
		return;
	} else if(2*vin+2>=n) { //左子节点非NULL 右子节点为NULL 
		path[pin+1]=level[2*vin+1]; //添加左子节点后 打印退出 
		printPath(pin+1);
		return;
	} else {
		dfs(2*vin+2,pin+1);
		dfs(2*vin+1,pin+1);
	}
}
int main(int argc,char * argv[]) {
	scanf("%d",&n);
	for(int i=0; i<n; i++) {
		scanf("%d",&level[i]);
	}
	dfs(0,0);
	for(int i=1;i<n;i++){
		if(level[(i-1)/2]>level[i])isMin=0; //如果i是从1存储的,这里应该是level[i/2]>level[i] 
		if(level[(i-1)/2]<level[i])isMax=0;
	}
	if(isMax==1) {
		printf("Max Heap
");
	} else if(isMin==1) {
		printf("Min Heap
");
	} else {
		printf("Not Heap
");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/houzm/p/12344810.html