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 ...
随机推荐
- 盘点Mac上搭建本地WebServer的几种方式
第一种: 通过Nginx搭建本地WebServer 安装nginx brew install nginx 安装完后在终端输入nginx指令,启动nginx查看效果 确定安装好之后,在根目录创建一个文件 ...
- HTTPS 笔记
随着互联网的迅速发展,网络安全问题日益凸显,现在 Chrome 浏览器已经开始阻止非 https 网站的访问了.对于 https 的流程一直不是十分清晰,借着还没有完全复工有时间,大概画了个图总结一下 ...
- python3编写程序,根据输入的行列数值,生成相应的矩阵(其中元素为随机数)。
代码如下: import random n = int(input("请输入行:")) m = int(input("请输入列:")) x = y = 0 wh ...
- BrowserSync(保存代码后,自动刷新浏览器)
摘要 Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面.更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项 ...
- 如何让antd的Modal组件的确认和取消不显示(或自定义按钮)(转载)
使用Modal中的footer属性,如下: <Modal title="更改成员" visible={visible} confirmLoading={confirmLoad ...
- yield 语法备忘录
yield 语法备忘录 yield 语法备忘录 语法 .net yield 读作:“一有得” 英式发音 皮一下~ yield 关键字向编译器指示它所在的方法是迭代器块. 编译器生成一个类来实现 ...
- Windows主机与centOS虚拟机之间"ping不通"
为什么要遇到这个问题 这是我重新安装centOS7.5虚拟机之后遇到的问题——我需要安装一个SecureCRT工具,结果主机与虚拟机没有ping通. 在安装这个工具之前需要进行主机与虚拟机的相互pin ...
- 矩阵快速幂-QuickPow
矩阵快速幂引入: 1.整数快速幂: 为了引出矩阵的快速幂,以及说明快速幂算法的好处,我们可以先求整数的幂.如果现在要算X^8:则 XXXXXXXX 按照寻常思路,一个一个往上面乘,则乘法运算进行7次. ...
- 环境篇:Docker
环境篇:Docker www.docker.com Docker 是什么? Docker 是一个开源的应用容器引擎,基于Go语言并遵从Apache协议的开源,让开发者可以打包他们的应用以及依赖包到一个 ...
- C# Linq方式生成SAP对接的XML格式内容(一般处理程序 ashx )
Linq生成XML的方法: string CreateXML(string strkey, string strDATAJSON) { XDeclaration dec = new XDeclarat ...