1043 Is It a Binary Search Tree (25分)(树的插入)
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 (≤). 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
题目分析:树的插入 因为给的是先序遍历 所以每次插入时 如果插入的是左子树 那么它的父亲的右节点必然为空 不然就插入失败 对于对称的情况也是类似
因此 我们只需要知道在插入时 每个节点左右子树是否存在就可
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
typedef struct Node* PtrToNode;
vector<int> V;
struct Node{
int data;
PtrToNode Left, Right;
bool LE, RI;
};
bool Insert(PtrToNode&T,int Element)
{
if (!T){
T = new Node;
T->data = Element;
T->Left = NULL;
T->Right = NULL;
T->LE = false;
T->RI = false;
}
else
if (Element < T->data){
if (T->RI)
return false;
else{
T->LE = true;
return Insert(T->Left, Element);
}
}
else{
T->RI = true;
return Insert(T->Right, Element);
}
return true;
}
bool InsertR(PtrToNode& T, int Element)
{
if (!T) {
T = new Node;
T->data = Element;
T->Left = NULL;
T->Right = NULL;
T->LE = false;
T->RI = false;
}
else
if (Element >=T->data) {
if (T->RI)
return false;
else {
T->LE = true;
return InsertR(T->Left, Element);
}
}
else {
T->RI = true;
return InsertR(T->Right, Element);
}
return true;
}
void PostOrder(PtrToNode T)
{
if(T)
{
PostOrder(T->Left);
PostOrder(T->Right);
V.push_back(T->data);
}
}
int main()
{
int N;
cin >> N;
int num;
PtrToNode TL = NULL, TR = NULL;
bool flagL,flagR;
flagL = flagR = true;
for (int i = ; i < N; i++)
{
cin >> num;
if(flagL)flagL = Insert(TL, num);
if(flagR)flagR = InsertR(TR, num);
if (!flagL&&!flagR)
break;
}
if (flagL||flagR)
{
cout << "YES"<<endl;
if (flagL)
PostOrder(TL);
else
PostOrder(TR);
for (auto it = V.begin(); it != (V.end() - ); it++)
cout << *it << " ";
cout << *(V.end() - );
}
else
cout << "NO"; }
1043 Is It a Binary Search Tree (25分)(树的插入)的更多相关文章
- PAT 甲级 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 bina ...
- PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. t ...
- PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1043. Is It a Binary Search Tree (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
- PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 【PAT】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 ...
随机推荐
- django 从零开始 3认识url解析
在视图函数中定义一个函数abc 接受得到的参数 并显示在页面上 urls中设置 在页面会显示出错误 找不到该url ,原因是django1版本中使用的是url和re_path ,突然django2变 ...
- Head First设计模式——蝇量和解释器模式
蝇量 蝇量模式:如果让某个类的一个实例能用来提供许多“虚拟实例”,就使用蝇量模式. 在一个设计房子的平台中,周围要加上一些树,树有一个坐标XY坐标位置,而且可以根据树的年龄动态将自己绘制出来.如果我们 ...
- go-admin基于Gin + Vue + Element UI的前后端分离权限管理系统
✨ 特性 遵循 RESTful API 设计规范 基于 GIN WEB API 框架,提供了丰富的中间件支持(用户认证.跨域.访问日志.追踪ID等) 基于Casbin的 RBAC 访问控制模型 JWT ...
- R调用C++示例
sourceCpp {Rcpp}:Source C++ Code from a File or String sourceCpp(file = "", code = NULL, e ...
- element UI中的tab切换栏
html代码:(用的是el-tab组件) <el-tabs v-model="activeIndex" type="border-card" @tab-c ...
- Jquery 系列化表单
大家知道Jquery中有serialize方法,可以将表单序列化为一个“&”连接的字符串,但却没有提供序列化为Json的方法.不过,我们可以写一个插件实现. 我在网上看到有人用替换的方法,先用 ...
- ECNU 计算机系统 (CSAPP) 教材习题作业答案集
这里是华东师范大学计算机系统的作业答案.由于几乎每一年布置的习题都几乎相同,网上的答案又比较分散,就把自己上学期提交的作业pdf放上来了,供参考. 长这样 Download Link:http://c ...
- 【Weiss】【第03章】练习3.13:桶排序
[练习3.13] 利用社会安全号码对学生记录构成的数组排序.编写一个程序进行这件工作,使用具有1000个桶的基数排序并且分三趟进行. Answer: 首先,对社会安全号码不了解的就把它当成一个不超过9 ...
- 使用C#+EmguCV处理图像入门(一)
首先我们先了解一下该库的一些相关信息 OpenCV(Open Source Computer Vision Library)是一个(开源免费)发行的跨平台计算机视觉库,可以运行在Linux.Windo ...
- git版本控制系统小白教程(上)
前言:本文主要介绍git版本控制系统的一些基础使用,适合小白入门,因为内容较多,会分为两部分进行分享. Git介绍 Git是目前世界上最先进的分布式版本控制系统.并且它是一个开源的分布式版本控制系 ...