题目地址: 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;
}