1. 这是二叉查找树BST那节的习题,要做出来这题,需要具备的基础知识有:BST的创建,涉及函数createBSTinsertBSTnewNode,二叉树的先序遍历、后序遍历。

2. 需要转过来的弯的有:

给定了序列,其实就可以创建出唯一的BST了。也就是一开始给定的那个序列,不要管他是什么序,反正就是个确定的序列。后面用来比对的时候再看他是不是先序,是不是镜像先序。

所谓镜像就是把检索左右子树的位置互换以下。

3. 一个非常使用的技巧:向量容器盛整数可以直接比对序列。详见妙用vector:根据第一个不等的元素比较两个序列大小的利器

4. 我犯了一个非常傻的错误,在遍历的时候第一句应该是root==NULL退出,写成了!=,导致我一直在纠结传进来的参数的引用写的对不对。也调试不出,后来猛然看见的。

AC代码

#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
typedef long long LL;using namespace std;const int maxn = 100010;struct Node{int data;Node *lchild,*rchild;
};Node* newNode(int v){Node* node = new Node;//申请变量的地址空间node->lchild = node->rchild = NULL;//新建的结点没有左右孩子node->data = v;//数据域为传进的参数值return node;
}void insertBST(Node* &root,int v){//由于要对二叉树的结构进行修改,所以传入根节点的引用if(root==NULL){//查找失败的位置,就是要插入的位置root = newNode(v);return;}if(v<root->data)insertBST(root->lchild,v);//根据BST的性质,此时往左子树搜索 else insertBST(root->rchild,v);
}Node* createBST(vector<int> data,int n){//data是待插入值域的数组,n是数组长度 Node* root = NULL;for(int i=0;i<n;i++){insertBST(root,data[i]);}return root;
}void preOrder(Node* root,vector<int>& preSeq){if(root==NULL)return;preSeq.push_back(root->data);preOrder(root->lchild,preSeq);preOrder(root->rchild,preSeq);
}void mirrorPreOrder(Node* root,vector<int>& mirrorPreSeq){if(root==NULL)return;mirrorPreSeq.push_back(root->data);mirrorPreOrder(root->rchild,mirrorPreSeq);mirrorPreOrder(root->lchild,mirrorPreSeq);}void postOrder(Node* root,vector<int>& postSeq){if(root==NULL)return;postOrder(root->lchild,postSeq);postOrder(root->rchild,postSeq);postSeq.push_back(root->data);
}void mirrorPostOrder(Node* root,vector<int>& mirrorPostSeq){if(root==NULL)return;mirrorPostOrder(root->rchild,mirrorPostSeq);mirrorPostOrder(root->lchild,mirrorPostSeq);mirrorPostSeq.push_back(root->data);
} int main(){int n,data;vector<int> initSeq,preSeq,mirrorPreSeq,postSeq,mirrorPostSeq;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&data);initSeq.push_back(data);}Node* root = createBST(initSeq,n);preOrder(root,preSeq);if(preSeq==initSeq){printf("YES\n");postOrder(root,postSeq);for(int i=0;i<postSeq.size();i++){printf("%d",postSeq[i]);if(i!=postSeq.size()-1)printf(" ");}return 0;}mirrorPreOrder(root,mirrorPreSeq);if(mirrorPreSeq==initSeq){printf("YES\n");mirrorPostOrder(root,mirrorPostSeq);for(int i=0;i<mirrorPostSeq.size();i++){printf("%d",mirrorPostSeq[i]);if(i!=mirrorPostSeq.size()-1)printf(" ");}return 0;}printf("NO"); return 0;
}

邪门,第二次做代码优化了,对于镜像居然检测不出

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<time.h>
#include<vector>
#include<set>
#include<string>
#include<tr1/unordered_map>using namespace std;
using namespace std::tr1;
typedef long long LL;const int maxn = 107;
const int MOD = 1000000007;
const int INF = 1000000000;//INF:下确界
const LL SUP = (1LL<<63)-1;//SUP:上确界
const double eps = 1e-5;struct Node{int v;Node *lchild,*rchild;
};void insert(int x,Node* &root){if(root==NULL){root = new Node;root->v = x;root->lchild = root->rchild = NULL;return;}if(x<root->v)insert(x,root->lchild);else insert(x,root->rchild);
}vector<int> initV,testV,testV2,postV;void preOrder(Node* root){if(root==NULL)return;testV.push_back(root->v);if(root->lchild!=NULL)preOrder(root->lchild);if(root->rchild!=NULL)preOrder(root->rchild);
}void preOrderMir(Node* root){if(root==NULL)return;testV2.push_back(root->v);if(root->rchild!=NULL)preOrder(root->rchild);if(root->lchild!=NULL)preOrder(root->lchild);
}void postOrder(Node* root){if(root==NULL)return;if(root->lchild!=NULL)postOrder(root->lchild);if(root->rchild!=NULL)postOrder(root->rchild);postV.push_back(root->v);
}void postOrderMir(Node* root){if(root==NULL)return;if(root->rchild!=NULL)postOrder(root->rchild);if(root->lchild!=NULL)postOrder(root->lchild);postV.push_back(root->v);
}int main(){int n,x;cin>>n;for(int i=0;i<n;i++){cin>>x;initV.push_back(x);}Node* root = NULL;for(int i=0;i<n;i++){insert(initV[i],root);}preOrder(root);if(testV==initV){printf("YES\n");postOrder(root);for(int i=0;i<postV.size();i++){printf("%d%s",postV[i],i==postV.size()-1?"\n":" ");}return 0;}preOrderMir(root);if(testV2==initV){printf("YES\n");postV.clear();postOrderMir(root);for(int i=0;i<postV.size();i++){printf("%d%s",postV[i],i==postV.size()-1?"\n":" ");}return 0;}printf("NO\n");return 0;
}

1043 Is It a Binary Search Tree相关推荐

  1. PAT甲级1043 Is It a Binary Search Tree :[C++题解]判断二叉搜索树BST、给定前序序列和中序序列

    文章目录 题目分析 题目链接 题目分析 二叉搜索树(BST):左子树小于根结点,右子树大于等于根结点. 二叉搜索树的中序遍历一定是有序序列.所谓中序遍历:先访问左子树,再访问根结点,最后访问右子树. ...

  2. C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)

    1043 Is It a Binary Search Tree (25分) A Binary Search Tree (BST) is recursively defined as a binary ...

  3. PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...

  4. 1043 Is It a Binary Search Tree(二叉查找树BST)

    1043 Is It a Binary Search Tree 0.题目 A Binary Search Tree (BST) is recursively defined as a binary t ...

  5. 【题意+分析】1043 Is It a Binary Search Tree (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A Binary Search Tree (BST) is recursively defined as a binary tre ...

  6. PTA甲级 1043 Is It a Binary Search Tree (25分) 树的遍历

    强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬 本文由参考于柳神博客写成 柳神的CSDN博客,这个可以搜索文章 柳神的个人博客,这个没有广告,但是不能搜索 还有就是非常非常有用的 算法笔记 全 ...

  7. 1043 Is It a Binary Search Tree (25 分)【难度: 中 / 知识点: 构造二叉搜索树(BST) 】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 首先二叉搜索树是左子树的元素都小于根,左子树 ...

  8. pat1043. Is It a Binary Search Tree (25)

    1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  9. 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)

    议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...

最新文章

  1. 在线教学生计算机,计算机系统基础—廖浩德老师—在线教学的先行者
  2. android 写字体投影,android之字体阴影效果
  3. word 2010中如何创建多级目录和多级列表
  4. 遇见C++ AMP:在GPU上做并行计算
  5. php表达式生成工具,thinkPHP5.0数据查询表达式生成技巧
  6. 专家观点:不断茁壮的音频生态系统中心(转)
  7. libopencv_core.a(persistence.cpp.o): undefined reference to symbol 'gzclose'
  8. cad一键标注闭合区域lisp_CAD快捷键大全,你值得学会!
  9. 每秒处理10万订单的支付架构
  10. linux安全技术课程报告,综合实例一+linux平台WEB安全技术研究报告.doc
  11. idea package自动生成_IDEA自动生成pojo实体类模板
  12. [转载] python 判断字符串是否包含另一个字符串_强烈推荐:Python字符串(string)方法整理(一)...
  13. 贴片电阻的功率与封装对照表
  14. 素材.html,素材标签.html
  15. 北京近期校园招聘java_JAVA研发工程师-校招,北京
  16. MySQL高级---04
  17. Windows取证——CHNTPW工具使用(可更改 Windows 密码)
  18. 应用现有EXCEL工作簿实现简单人事管理
  19. STM32夺命100问,你知道几个?
  20. 7所大学提供区块链、加密货币及金融科技相关线上课程

热门文章

  1. 小程序在wxml使用indexOf
  2. 图解5G NR帧结构
  3. 【快速入门系列】简述 for...in 和 for...of 区别
  4. 写一篇C语言入门第一讲
  5. Top 15 不起眼却有大作用的 .NET功能集
  6. (转) 地区赛获胜策略,赛前默念!
  7. Winform开发的界面处理优化
  8. 关于IOS获取本地通讯录信息(包含iOS9.0前后)
  9. MySQL的登陆错误:ERROR 1049 (42000): Unknown database 'root'
  10. centos 6.* 修改时间