A1043 Is It a Binary Search Tree(二叉搜索树)


题目地址: https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856

遇到的问题

1 指针定义之后要赋值NULL(空指针)

大家看这段代码

node* root;
if (root == NULL) {//空树,即为插入位置
	root = NewNode(x);
	return true;
}

但是怎么也无法插入,后来发现root!=NULL, 必须这样写:

node* root = NULL;
if (root == NULL) {//空树,即为插入位置
	root = NewNode(x);
	return true;
}

2 vector可以直接进行比较

vector可以直接进行比较,我居然还写了一个cmp()函数

//比较两个vector
bool cmp(vector<ElemType> a, vector<ElemType> b) {
	if (a.size() != b.size())
		return false;
	for (int i = 0; i < a.size(); i++) {
		if (a[i] != b[i])
			return false;
	}

	return true;
}

3 复制粘贴要注意

最后有一个错我怎么都找不到,大家看看下面的一个代码。

bool insert_mirror(node* &root, ElemType x) {
	if (root == NULL) {//空树,即为插入位置
		root = NewNode(x);
		return true;
	}

	//if (x == root->num)	//已有节点,不需要插入
	//	return true;
	if (x == 8 || x==11)
		int a = 0;
	if (x >= root->num) {
		insert(root->lchild, x);
	}
	else {
		insert(root->rchild, x);
	}
	return true;
}

我把insert改成insert_mirror就没问题了,真是以后复制粘贴时要检查一下。

完整代码

#include <iostream>
#include <string>
#include <set>
#include <map>
#include<queue>
#include<stack>
#include<algorithm>

//new and delete
//A1043 Is It a Binary Search Tree
using namespace std;

typedef int ElemType;
struct node {
	ElemType num;	
	node* lchild;
	node* rchild;
};

node* NewNode(ElemType x) {
	node* Node = new node;
	Node->lchild = Node->rchild = NULL;
	Node->num = x;
	return Node;
}
//插入元素为x的结点
bool insert(node* &root, ElemType x) {
	if (root == NULL) {//空树,即为插入位置
		root = NewNode(x);
		return true;
	}

	//if (x == root->num)	//已有节点,不需要插入
	//	return true;
	if (x < root->num) {
		insert(root->lchild, x);
	}
	else {
		insert(root->rchild, x);
	}
	return true;
}
bool insert_mirror(node* &root, ElemType x) {
	if (root == NULL) {//空树,即为插入位置
		root = NewNode(x);
		return true;
	}

	//if (x == root->num)	//已有节点,不需要插入
	//	return true;
	if (x == 8 || x==11)
		int a = 0;
	if (x >= root->num) {
		insert_mirror(root->lchild, x);
	}
	else {
		insert_mirror(root->rchild, x);
	}
	return true;
}
//Get the mirror Binary Search Tree
node* GetMirror(vector<ElemType> seq) {
	node* root=NULL;
	for (int i = 0; i < seq.size(); i++)
		insert_mirror(root, seq[i]);
	return root;
}

//PreOrder 循环实现
vector<ElemType> preOrder(node* root) {
	vector<ElemType> set;
	stack<node*>s;
	node* p = root;

	//根,左,右
	while (s.size() != 0 || p != NULL) {
		if (p != NULL) {
			//访问
			set.push_back(p->num);
			s.push(p);
			p = p->lchild;
		}
		else {
			p = s.top();
			p = p->rchild;
			s.pop();
		}
	}

	return set;
}
//PostOrder 递归实现
void postOrder(node* root, vector<ElemType> &set) {
	if (root == NULL)
		return;
	postOrder(root->lchild, set);
	postOrder(root->rchild, set);
	set.push_back(root->num);
	return;

}

void PrintS(vector<ElemType> a) {
	for (int i = 0; i < a.size(); i++)
		cout << a[i] << " ";
	cout << endl;

}
int main() {
	int N;
	cin >> N;
	vector<ElemType> seq;	//sequence
	node * root=NULL,* root_m=NULL;			//Binary Search Tree
	for (int i = 0; i < N; i++) {
		ElemType temp;
		cin >> temp;
		seq.push_back(temp);
		insert(root, temp);
	}

	root_m = GetMirror(seq);
	//preOr: 先序遍历序列;
	//preOr_m: 镜像树先序遍历序列
	vector<ElemType> preOr, preOr_m,postOr;	
	preOr = preOrder(root);
	preOr_m = preOrder(root_m);

	if (preOr==seq) {
		cout << "YES\n";
		postOrder(root, postOr);
		for (int i = 0; i < postOr.size(); i++) {
			cout << postOr[i];
			if (i != postOr.size() - 1)
				cout << " ";
			else
				cout << endl;
		}
	}
	else if (preOr_m==seq) {
		cout << "YES\n";
		postOrder(root_m, postOr);
		for (int i = 0; i < postOr.size(); i++) {
			cout << postOr[i];
			if (i != postOr.size() - 1)
				cout << " ";
			else
				cout << endl;
		}
	}
	else {
		cout << "NO\n";
	}

	return 0;
}
以下是实现该功能的 C 代码: ```c #include <stdio.h> #include <stdbool.h> #include <stdlib.h> // 定义二叉树结构体 struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; // 中序遍历二叉树并判断是否为二叉搜索树 bool inorderTraversal(struct TreeNode *root, int *prev) { if (root == NULL) { return true; } if (!inorderTraversal(root->left, prev)) { return false; } if (root->val <= *prev) { return false; } *prev = root->val; return inorderTraversal(root->right, prev); } bool isValidBST(struct TreeNode* root){ int prev = INT_MIN; return inorderTraversal(root, &prev); } // 创建二叉树 struct TreeNode* createTree(int *arr, int start, int end) { if (start > end) { return NULL; } int mid = (start + end) / 2; struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = arr[mid]; root->left = createTree(arr, start, mid-1); root->right = createTree(arr, mid+1, end); return root; } // 打印中序遍历序列 void inorderPrint(struct TreeNode *root) { if (root == NULL) { return; } inorderPrint(root->left); printf("%d ", root->val); inorderPrint(root->right); } // 测试代码 int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7}; int len = sizeof(arr) / sizeof(arr[0]); struct TreeNode *root = createTree(arr, 0, len-1); inorderPrint(root); printf("\nThis tree is %s a binary search tree.\n", isValidBST(root) ? "" : "not"); return 0; } ``` 该程序首先利用给定的数组创建一棵平衡二叉搜索树,然后打印出它的中序遍历序列。最后,利用 `inorderTraversal` 函数判断该二叉树是否为二叉搜索树。 需要注意的是,该程序中的 `inorderTraversal` 函数利用了指向前驱节点的指针 `prev`,用于判断当前节点和它的前驱节点的大小关系。在最开始调用 `isValidBST` 函数时,将 `prev` 初始化为整型最小值 `INT_MIN`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值