A1043. Is It a Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.
Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input 1:
7
8 6 5 7 10 8 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:
NO
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
typedef struct NODE{
struct NODE *lchild, *rchild;
int key;
}node;
int N;
vector<int> keys, pre, preM, ans;
void insert(node* &root, int key){
if(root == NULL){
root = new node;
root->key = key;
root->lchild = NULL;
root->rchild = NULL;
return;
}
if(key >= root->key){
insert(root->rchild, key);
}else{
insert(root->lchild, key);
}
}
node* create(vector<int> &keys){
node* root = NULL;
for(int i = ; i < N; i++)
insert(root, keys[i]);
return root;
}
void preOrder(node* root){
if(root == NULL)
return;
pre.push_back(root->key);
preOrder(root->lchild);
preOrder(root->rchild);
}
void preOrder2(node* root){
if(root == NULL)
return;
preM.push_back(root->key);
preOrder2(root->rchild);
preOrder2(root->lchild);
}
void postOrder(node* root){
if(root == NULL)
return;
postOrder(root->lchild);
postOrder(root->rchild);
ans.push_back(root->key);
}
void postOrder2(node* root){
if(root == NULL)
return;
postOrder2(root->rchild);
postOrder2(root->lchild);
ans.push_back(root->key);
} int main(){
int temp;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &temp);
keys.push_back(temp);
}
node* root = create(keys);
preOrder(root);
preOrder2(root);
if(pre == keys ){
printf("YES\n");
postOrder(root);
for(int i = ; i < N; i++){
if(i != N - )
printf("%d ", ans[i]);
else printf("%d", ans[i]);
}
}else if(preM == keys){
printf("YES\n");
postOrder2(root);
for(int i = ; i < N; i++){
if(i != N - )
printf("%d ", ans[i]);
else printf("%d", ans[i]);
}
}else {
printf("NO\n");
}
cin >> N;
return ;
}
总结:
1、题意:给出一组key,先按照给出的顺序建立搜索树。再对其本身和他的镜像进行先序遍历,看看是否先序遍历的序列和给出的一组key顺序相同。
2、对逆转的镜像树,可以不必实际上逆转该树,而仅仅在先序和后序访问左右子树时,从原来的先左后右变成先右后左。
3、两个vector在元素为int时可以直接比较。
4、注意字符串不要打错,“NO”打成“No”结果检查好久。
5、本题中允许搜索树中有重复的key,在定义中右子树大于等于根节点。 recursively:递归地
A1043. Is It a Binary Search Tree的更多相关文章
- A1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- A1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲级——A1043 Is It a Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT_A1043#Is It a Binary Search Tree
Source: PAT A1043 Is It a Binary Search Tree (25 分) Description: A Binary Search Tree (BST) is recur ...
- 1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
随机推荐
- bootstrap模态框动态赋值, ajax异步请求数据后给id为queryInfo的模态框赋值并弹出模态框(JS)
/查询单个 function query(id) { $.ajax({ url : "/small/productServlet", async : true, type : &q ...
- 源码追踪,解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题
在windows系统本地运行spark的wordcount程序,会出现一个异常,但不影响现有程序运行. >>提君博客原创 http://www.cnblogs.com/tijun/ & ...
- Plugin/Preset files are not allowed to export objects,webpack报错/babel报错的解决方法
1.为什么会报错 ? 这里抱着错误是因为 babel 的版本冲突. 多是因为你的 babel 依赖包不兼容. 可以查看你的 package.json 的依赖列表 即有 babel 7.0 版本的( @ ...
- idea使用破解版mybatis plugin插件失败,idea打不开的解决方案
记一次错误解决方案 打开 idea.vmoptions (Help -> Edit Custom VM Options...) ,在这里进行了修改 加了破解jar包的路径,但是之前的路径中有中文 ...
- Supervisord管理进程实践
今天凑空研究了下Supervisord,这是一款linux进程管理工具,使用python开发,主要用于在后台维护进程(类似master守护进程),可以实现监控进程的状态.自动重启进程等操作,便于一些服 ...
- How to blog on Github
git clone https://github.com/test/test.github.io.git cd ~/test.github.io git config --global push.de ...
- vuex2.0 基本使用(3) --- getter
有的组件中获取到 store 中的state, 需要对进行加工才能使用,computed 属性中就需要写操作函数,如果有多个组件中都需要进行这个操作,那么在各个组件中都写相同的函数,那就非常麻烦,这 ...
- 【XSY2760】nonintersect 计算几何
题目描述 平面上有\(n\)条线段,你要擦掉所有线段但保留原有的\(2n\)个端点,然后连接这些端点形成\(n\)条不相交的线段,每个端点只能在一条线段中. 假设你画的线段总长为\(Y\),原有线段的 ...
- Ionic生命周期与注意点
需要注意的地方是:在走页面的生命周期以前,会先走构造方法 构造方法只走一次,除非再次创建这个页面.所以如果先push了一个新页面,然后再调用pop()返回到之前的页面,那么是不会走构造方法里面的方法的 ...
- ansible 开源批量管理服务器工具
Ansible 是一款基于 Python 开发的自动化运维工具,可以进行配置管理.批量部署等功能.对于机器较多的场景,可以使用 Ansible 来免去重复敲命令的烦恼. 安装ansibleyum -y ...