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 ...
随机推荐
- Linux下MongoDB单实例的安装和配置详解
推荐网站 MongoDB官网:http://www.mongodb.org/ MongoDB学习网站:http://www.runoob.com/mongodb 一.创建MongoDB的资源目录和安装 ...
- rabitmq + php
消费者 <?php //配置信息 $conn_args = array( 'host' => '127.0.0.1', 'port' => '5672', 'login' => ...
- 在 centos6 上安装 LAMP
LAMP 代表的是 Linux, Apache, MySQL, 以及 PHP. 第一步,安装 Apache 使用 yum 安装 sudo yum install httpd 启动 httpd 服务 ...
- python安装包的3的方式
1.pip pip install 包名 2.压缩包(针对pip安装不上) 1.下载源码解压(压缩包有setup.py) 2.python setup.py install 3.****.whl文件 ...
- java猜数游戏(新手记录每天的作业)
//导入包 import java.util.Scanner;import java.util.Random; //定义一个类 public class Zcs{ //公共静态的主方法 public ...
- Natas30 Writeup(sql注入)
Natas30: 本关是一个登录页面,查看源码,可以发现关键代码. if ('POST' eq request_method && param('username') &&am ...
- Arch Linux安装配置-双系统(1)
Arch Linux启动盘准备: 在Windows下安装Win32 Disk Imager,打开页面,点击Download即可! 安装配置 1.选择我同意 2.选择安装位置路径 3.打勾,在桌面显示图 ...
- org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'employeeId' not found. Available parameters are [page, map, param1, param2] 解决方法
原因很简单就是没映射到接口添加 @Param 注解 ->@Param("map") 然后在mapper.xml map.employeeId 再次测试 已经解决 ->
- SQLServer——MASTER..spt_values
常常见到这个表,人家用得天花乱坠的. 自己select一看却莫名其妙的. 如上, 这个表主要用来保存一些枚举值, 据说是从sybase继承过来,许多函数和存储过程可以看到它的身影.也可以叫系统常量表吧 ...
- 1.Lambda表达式
1.Lambda表达式 语法糖 也叫作糖衣语法,增强了代码的可读性 避免了出错的机会 但是,这种语法对于语言的功能并没有增强 和Lambda一样的糖衣语法还有:(1)泛型 <>(2)自动装 ...