PAT甲级 1043 Is It a Binary Search Tree

原题链接:

https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId=994805440976633856&page=0

注意点:

  1. 这里的代码在PAT能过,AcWing会出现超时的测试点。

代码:

#include <iostream>
#include <vector>
using namespace std;

#define endl '\n'

int N;
struct treeNode {
	int val;
	treeNode* left;
	treeNode* right;
};
treeNode* root;
int value[1000];
bool isBST;
bool isMirror;

bool isBSTPre(int left,int right) {//左闭右开
	int rootVal = value[left];
	int pos = left + 1;
	if (left >= right) {
		return true;
	}
	while (value[pos] < rootVal&&pos<N) {
		pos++;
	}
	int pos1 = pos;
	while (value[pos1] >= rootVal&&pos1<N) {
		pos1++;
	}
	if (pos == N || pos1 == N) {
		return isBSTPre(left + 1, pos) && isBSTPre(pos, pos1);
	}
	else {
		return false;
	}
}

bool isMirrorPre(int left, int right) {
	int rootVal = value[left];
	int pos = left + 1;
	if (left >= right) {
		return true;
	}
	while (value[pos] >= rootVal && pos < N) {
		pos++;
	}
	int pos1 = pos;
	while (value[pos1] < rootVal && pos1 < N) {
		pos1++;
	}
	if (pos == N || pos1 == N) {
		return isMirrorPre(left + 1, pos) && isMirrorPre(pos, pos1);
	}
	else {
		return false;
	}
}

void bstInsert(treeNode*& now, int x) {
	if (now == NULL) {
		now = new treeNode;
		now->val = x;
		now->left = NULL;
		now->right = NULL;
		return;
	}
	if (x < now->val) {//插入左子树
		bstInsert(now->left, x);
	}
	else {//插入右子树
		bstInsert(now->right, x);
	}
}

void mirrorInsert(treeNode*& now, int x) {
	if (now == NULL) {
		now = new treeNode;
		now->val = x;
		now->left = NULL;
		now->right = NULL;
		return;
	}
	if (x >= now->val) {//插入左子树
		mirrorInsert(now->left, x);
	}
	else {//插入右子树
		mirrorInsert(now->right, x);
	}
}

int x = 0;
void postTraversal(treeNode *now) {
	if (now == NULL) {
		return;
	}
	else {
		postTraversal(now->left);
		postTraversal(now->right);
		if (x != 0) {
			cout << " ";
		}
		cout << now->val;
		x++;
	}
}


signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> value[i];
	}
	isBST=isBSTPre(0,N);
	if (!isBST) {
		isMirror=isMirrorPre(0,N);
	}
	if (isBST || isMirror) {
		cout << "YES" << endl;
		if (isBST) {
			for (int i = 0; i < N; i++) {
				bstInsert(root,value[i]);
			}
		}
		else {
			for (int i = 0; i < N; i++) {
				mirrorInsert(root, value[i]);
			}
		}
		postTraversal(root);
	}
	else {
		cout << "NO" << endl;
	}

	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用C语言实现的代码,判断一棵二叉树是否为完全二叉树。 ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; typedef struct Queue { TreeNode **data; int front; int rear; int size; } Queue; Queue *createQueue(int size) { Queue *q = (Queue *)malloc(sizeof(Queue)); q->data = (TreeNode **)malloc(sizeof(TreeNode *) * size); q->front = q->rear = 0; q->size = size; return q; } bool isEmpty(Queue *q) { return q->front == q->rear; } bool isFull(Queue *q) { return (q->rear + 1) % q->size == q->front; } void enqueue(Queue *q, TreeNode *node) { if (isFull(q)) { return; } q->data[q->rear] = node; q->rear = (q->rear + 1) % q->size; } TreeNode *dequeue(Queue *q) { if (isEmpty(q)) { return NULL; } TreeNode *node = q->data[q->front]; q->front = (q->front + 1) % q->size; return node; } bool isCompleteTree(TreeNode* root) { if (root == NULL) { return true; } Queue *q = createQueue(1000); bool flag = false; enqueue(q, root); while (!isEmpty(q)) { TreeNode *node = dequeue(q); if (node->left) { if (flag) { return false; } enqueue(q, node->left); } else { flag = true; } if (node->right) { if (flag) { return false; } enqueue(q, node->right); } else { flag = true; } } return true; } int main() { TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = 1; root->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->val = 2; root->right = (TreeNode *)malloc(sizeof(TreeNode)); root->right->val = 3; root->left->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right = (TreeNode *)malloc(sizeof(TreeNode)); root->left->right->val = 5; root->right->left = (TreeNode *)malloc(sizeof(TreeNode)); root->right->left->val = 6; printf("%s\n", isCompleteTree(root) ? "true" : "false"); return 0; } ``` 代码中使用了队列来存储二叉树中的节点,判断是否为完全二叉树的方法是,从根节点开始,每层的节点必须都存在,否则后面的节点都必须是叶子节点才满足完全二叉树的定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值