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

本文探讨了C++中二叉树的构建与遍历,重点讲解了指针初始化、vector比较及代码复用的重要性。通过具体实例,展示了如何避免常见错误,如未初始化指针和不当的代码复制粘贴。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目地址: 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值