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 ...
 
随机推荐
- 设计模式-15命令模式(Command Pattern)
			
1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...
 - 编译putty 源码去掉 Are you sure you want to close this session? 提示
			
0, 为什么要编译 putty ?在关闭窗口的时候,会弹出一个 Are you sure you want to close this session?要把这个去掉.当然也可以用 OD 之类的来修改. ...
 - video标签加载视频有声音却黑屏
			
问题 昨天用户上传了一个视频文件,然而发现虽然有声音但是黑屏. 解释 因为原视频的编码是用 mp4v 格式的,它需要专用的解码器.而 chrome 并不支持,所以无法播放. 然后如果用转码功能转成用 ...
 - CSS核心概念之盒子模型
			
盒子模型(Box Model) 关于更多CSS核心概念的文章请关注GitHub--CSS核心概念. 当对一个文档进行布局的时候,浏览器的渲染引擎会根据标准之一的 CSS 基础框盒模型(CSS basi ...
 - vim不能使用小键盘
			
使用终端登录Linux后使用vim编辑文本,这时在默认设置下载插入模式使用小键盘会插入一些非数字的字符. 更改的方法: 在终端设置中选择终端类型为linux
 - Jupyter NoteBook 系列之  安装启动和常用设置
			
介绍 Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,目前支持运行 40 多种编程语言. Jupyter Notebook 的本质是一个 Web 应 ...
 - sql语句的基本用法总结
			
一.sql语法 select */列名1,列名2... from 表名[连接查询 内连接/左连接 on条件] 必选的 where 条件 子查询/in/exists/between ... and .. ...
 - Journal of Proteome Research | Lipidomics reveals similar changes in serum phospholipid signatures of overweight and obese paediatric subjects (分享人:赵倩倩)
			
文献名:Lipidomics reveals similar changes in serum phospholipid signatures of overweight and obese paed ...
 - adt-bundle环境搭建(Win7+Win10)
			
一.adt-bundle安装包 安装包的下载地址:http://tools.android-studio.org/index.php/adt-bundle-plugin 链接中包含有windows. ...
 - Django之CBV装饰器,跨站请求伪造,auth认证
			
CBV加装饰器 基于session实现登录 def login(request): if request.method == 'POST': username = request.POST.get(' ...