首页 > 其他分享 >1043 Is It a Binary Search Tree——PAT甲级

1043 Is It a Binary Search Tree——PAT甲级

时间:2024-09-29 19:51:14浏览次数:3  
标签:1043 Binary Search right node val line null left

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

 solution:
 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
vector<int>a;
vector<int>pre,premir,post,postmir;
#define null NULL
struct node
{
	int val;
	node *left;
	node *right;
};
void build(node* &t,int x)
{
	if(t==null)
	{
		t=new node();
		t->val=x;
		t->left=null;
		t->right=null;
		return;
	}
	if(t->val<=x)build(t->right,x);
	else build(t->left,x);
}
void preorder(node *t)
{
	if(t==null)return;
	pre.push_back(t->val);
	preorder(t->left);
	preorder(t->right);
}
void premirorder(node *t)
{
	if(t==null)return;
	premir.push_back(t->val);
	premirorder(t->right);
	premirorder(t->left);
}
void postorder(node *t)
{
	if(t==null)return;
	postorder(t->left);
	postorder(t->right);
	post.push_back(t->val);
}
void postmirorder(node *t)
{
	if(t==null)return;
	postmirorder(t->right);
	postmirorder(t->left);
	postmir.push_back(t->val);
}
int main()
{
	int n;cin>>n;
	node *root=null;
	for(int i=0;i<n;i++)
	{
		int x;cin>>x;
		a.push_back(x);
		build(root,x);
	}
	premirorder(root);
	preorder(root);
	if(a==pre)
	{
		cout<<"YES"<<endl;
		postorder(root);
		for(int i=0;i<post.size();i++)
		{
			if(i)cout<<' ';
			cout<<post[i];
		}
		cout<<endl;
	}
	else if(a==premir)
	{
		cout<<"YES"<<endl;
		postmirorder(root);
		for(int i=0;i<postmir.size();i++)
		{
			if(i)cout<<' ';
			cout<<postmir[i];
		}
		cout<<endl;
	}
	else cout<<"NO"<<endl;
}

标签:1043,Binary,Search,right,node,val,line,null,left
From: https://blog.csdn.net/flyidj/article/details/142638853

相关文章

  • CentOS 7.9安装ElasticSearch7.14.0、ElasticSearch-Head、Kibana、Node14.18.2
    CentOS7.9安装ElasticSearch7.14.0、ElasticSearch-Head、Kibana、Node14.18.2 1.安装文件1.elasticsearch-7.14.0-linux-x8664.tar.gz2.elasticsearch-head-master.zip3.jdk-11linux-x64bin.tar.gz4.kibana-7.14.0-linux-x8664.tar.gz5.node-v14.18.2-linux-......
  • 自己封装Elasticsearch,下载到本地仓库复用
    拉取代码git拉取yxh-elasticsearch:es基本封装工具拉完修改你可以根据自己去修改这些代码,最后install加package,就打到了本地maven仓库,调用的时候也非常方便,可以看下文。介绍注解一共有三个自定义注解@DocumentIndex作用一共四个字段,indexName是用于你在实......
  • ElasticSearch初体验
    我的网站集成ElasticSearch初体验   最近,我给我的网站(https://www.xiandanplay.com/)尝试集成了一下es来实现我的一个搜索功能,因为这个是我第一次了解运用elastic,所以如果有不对的地方,大家可以指出来,话不多说,先看看我的一个大致流程   这里我采用的sdk的版本是El......
  • ElasticSearch倒排索引
    一、ElasticSearch基本概念        Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsear......
  • thinkphp项目中集成使用 Elasticsearch
    文章目录前言1.安装Elasticsearch2.安装ElasticsearchPHP客户端3.配置Elasticsearch连接4.使用Elasticsearch5.注意事项总结前言在ThinkPHP项目中集成使用Elasticsearch,你需要遵循几个步骤来确保Elasticsearch能够顺利地在你的项目中运行。以下是一个......
  • 电商API数据接口1688alibaba接口item_search_shop-获得店铺的所有商品接入演示
    一、接口功能item_search_shop接口是1688阿里巴巴提供的获取店铺所有商品的API接口,用户可以通过输入店铺ID,获取该店铺的所有商品信息。二、接口调用请求参数:seller_nick=b2b-2200733087881719de&start_price=0&end_price=0&q=&page=1&cid=参数说明:seller_nick:sid或者加密后的_sopi......
  • MySQL variables:binary-as-hex
    不注意到这个变化的话,还挺折腾人的。在MySQL8.0.19ReleaseNotes里,有这么一段话:Whenthemysqlclientoperatesininteractivemode,the--binary-as-hexoptionnowisenabledbydefault.Inaddition,outputfromthestatus(or\s)commandincludesthislinewhenth......
  • centos7安装elasticsearch6.3.x集群
    一、环境信息及安装前准备主机角色(内存不要小于1G): 软件及版本(百度网盘链接地址和密码:链接:https://pan.baidu.com/s/17bYc8MRw54GWCQCXR6pKjg提取码:f6w8)  部署前操作:关闭防火墙,关闭selinux(生产环境按需关闭或打开)同步服务器时间,选择公网ntpd服务器或者自建ntpd服务器......
  • 树状数组(Binary Indexed Tree, BIT)
    树状数组(BinaryIndexedTree,BIT)树状数组(BinaryIndexedTree,BIT),也称为FenwickTree,是一种用于高效处理数组前缀和查询和单点更新的数据结构。它能够在(O(\logn))时间内完成单点更新和前缀和查询操作。基本概念前缀和:给定一个数组a,前缀和prefix_sum[i]表示a[0]+......
  • ELK中日志数据采集器Filebeat的安装和使用、Filebeat结合Logstash进行日志处理入Elast
    一、ELK中日志数据采集器Filebeat的安装和使用    Beats是数据采集的得力工具,Beats能够将数据转发至Logstash进行转换和解析。Filebeat是Beats中的一种,Filebeat是本地文件的日志数据采集器,可监控日志目录或特定日志文件(tailfile),并将它们转发给Elasticsearch或Logstats......