C++学习之路 | PTA(甲级)—— 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 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

思路:
根据题目数据先建立二叉搜索树;
进行判断:
1.是否是先序遍历结果。
2.是否是镜像的先序遍历结果。

#include<iostream>
#include<vector>
using namespace std;
struct node {int data;node* l;node* r;
};
vector<int>pre, mirror_pre, post;
void Insert(struct node*& root, int a)//&root,c++引用;
{if (root == nullptr){root = new node();root->data = a;return;}if (a < root->data)Insert(root->l, a);else Insert(root->r, a);return;
}
struct node* create_tree(vector<int>v)
{struct node* root = nullptr;for (int i = 0; i < v.size(); i++)//二叉搜索树建设{Insert(root, v[i]);}return root;
}
void preorder(struct node* root)//先序遍历,结果保存在pre数组中
{if (root){pre.push_back(root->data);preorder(root->l);preorder(root->r);}
}
void postorder(struct node* root)//后序遍历,结果保存在post数组
{if (root){postorder(root->l);postorder(root->r);post.push_back(root->data);}
}
void mirror_preorder(struct node* root)//镜像先序遍历,结果保存在mirror_pre数组中
{if (root){mirror_pre.push_back(root->data);mirror_preorder(root->r);mirror_preorder(root->l);}
}
void mirror_postorder(struct node* root)//镜像后序遍历,结果保存在post数组
{if (root){mirror_postorder(root->r);mirror_postorder(root->l);post.push_back(root->data);}
}
int main()
{int n;cin >> n;vector<int>v(n);//v存储给定的数组for (int i = 0; i < n; i++){cin >> v[i];}struct node* root = create_tree(v);//建树preorder(root);//先进行先序遍历,保存结果。mirror_preorder(root);//保存镜像先序遍历值if (v == pre)//如果先序遍历数组和题目给定的判别数组相同{postorder(root);//保存后续遍历数组cout << "YES" << endl;for (int i = 0; i < post.size(); i++){if (i != 0)cout << " ";cout << post[i];}}else if (v == mirror_pre)//如果镜像先序遍历数组和题目给定的判别数组相同{mirror_postorder(root);//保存镜像的后序遍历数组cout << "YES" << endl;for (int i = 0; i < post.size(); i++){if (i != 0)cout << " ";cout << post[i];}}elsecout << "NO";
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/497110.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Proxy 和 Stairway To Heaven 模式

Proxy 和 Stairway To Heaven 模式

C++学习之路 | PTA(甲级)—— 1064 Complete Binary Search Tree (30分)(带注释)(精简)

1064 Complete Binary Search Tree (30分) 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 c…

Python自省(反射) 与 inspect 模块

Python 自省指南&#xff1a;https://www.ibm.com/developerworks/cn/linux/l-pyint/ From&#xff1a;https://my.oschina.net/taisha/blog/55597 在计算机编程中&#xff0c;自省是指这种能力&#xff1a;检查某些事物以确定它是什么、它知道什么以及它能做什么。即 列出对…

Snap Shots 出了新东西

今天收到 Snap Shots 的邮件&#xff0c;说是他出了新产品。我看了一下&#xff0c;是两个&#xff0c;一个是 RSS 聚合&#xff0c;很好的东西&#xff0c;以后可以预览 RSS 了。另外一个是 Profile preview。感觉第二个对我的吸引大一点&#xff0c;如果可以做一个网站专门提…

视频监控成AI芯片主战场,海康威视和大华股份占据半壁江山

来源&#xff1a;MEMS概要&#xff1a;图像和视频的人工智能处理&#xff0c;是目前AI芯片商业化前景最乐观的赛道&#xff0c;也是玩家们弯道超车的最佳机会。 图像和视频的人工智能处理&#xff0c;是目前AI芯片商业化前景最乐观的赛道&#xff0c;也是玩家们弯道超车的最佳机…

需求分析设计

规格说明 1、有些雇员时钟点工。会按照他们雇员记录中每小时报酬字段的值对他们进行支付。他们每天会提交工作时间卡&#xff0c;其中记录了日期以及工作小时数。如果他们每天工作超过8小时。那么超过的部分会按照正常报酬1.5倍进行支付。每周五对他们进行支付。 2、有些雇员…

C++学习之路 | PTA(甲级)—— 1099 Build A Binary Search Tree (30分)(带注释)(精简)

1099 Build A Binary Search Tree (30分) 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 co…

python 命令行 解析模块 optparse、argparse

optparse&#xff1a;https://docs.python.org/zh-cn/3/library/optparse.htmlargparse &#xff1a;https://docs.python.org/zh-cn/3/library/argparse.html3.2 版后已移除 optparse 模块&#xff0c;并且将不再继续开发&#xff1b;开发转至 argparse 模块进行。 ​1、argpa…

构建插件式的应用程序框架(六)----通讯机制(ZT)

前天发了构建插件式的应用程序框架(五)&#xff0d;&#xff0d;&#xff0d;&#xff0d;管理插件这篇文章&#xff0c;有几个朋友在回复中希望了解插件之间是如何通讯的。这个系列的文章写到这里&#xff0c;也该谈谈这个问题了&#xff0c;毕竟已经有了插件管理。不知道大家…

人工智能产业2018年待解的三大难题

来源&#xff1a;人民邮电报概要&#xff1a;2017年&#xff0c;人工智能领域在算法、政策、资金等方面已经出现了三大突破&#xff0c;业界欢欣鼓舞的情形很像1999年年底网络泡沫泛滥时的情形。2017年&#xff0c;人工智能领域在算法、政策、资金等方面已经出现了三大突破&…

矩阵连乘问题(c++)

矩阵连乘问题 问题描述&#xff1a; 给定n个矩阵&#xff1a;A1,A2,…,An&#xff0c;其中Ai与Ai1是可乘的&#xff0c;i1&#xff0c;2…&#xff0c;n-1。确定计算矩阵连乘积的计算次序&#xff0c;使得依此次序计算矩阵连乘积需要的数乘次数最少。输入数据为矩阵个数和每个矩…

我在犹豫是不是该收集这几首MP3

我在犹豫是不是该收集这几首MP3&#xff0c;确实&#xff0c;曲子歌词都非常不错。可我找不到收藏的理由。总是一个想法&#xff0c;让我放弃和错过了很多东西&#xff1a;我已经错过和失去了很多&#xff0c;这一次又算什么呢&#xff1f;这样的想法让我在面对很多人或事都已经…

QuestMobile 2017年中国移动互联网年度报告

来源&#xff1a;QuestMobile2017年&#xff0c;科技的风口兜兜转转&#xff0c;从直播、VR到AI再到区块链、短视频泛娱乐IP&#xff0c;最终在2017年底定格在了知识付费上&#xff0c;然而这并没有结束&#xff0c;紧随知识付费而来的就是撒币、大撒币……这就是中国移动互联网…

Python 读写配置文件模块: configobj 和 configParser

参考&#xff1a;http://www.voidspace.org.uk/python/configobj.html Python模块之ConfigParser - 读写配置文件&#xff1a;http://www.cnblogs.com/victorwu/p/5762931.html Python 官网 configparser 文档&#xff1a;https://docs.python.org/3.7/library/configparser.…

快速排序(c++)

1、快速排序的思想 快速排序就是给基准数据找在数组中正确位置的过程&#xff0c;一旦基准位置的正确位置找到&#xff0c;那基准位置左右两边经过同样的步骤递归也可以有序&#xff0c;最终整体数组有序。 整体可以理解为三个步骤&#xff1a; 1、先从队尾开始向前扫描且当l …

设计模式之禅--思维导图

原图ProcessOn里搜索&#xff1a;设计模式之禅

有BRT,为啥还建公交港湾

原来快速公交和普通公交要一块儿跑历山路公交港湾示意图(制图&#xff1a;赵国陆&#xff09;   “历山路上既然跑快速公交车&#xff0c;有BRT站台&#xff0c;还要公交港湾干吗&#xff1f;”21日&#xff0c;本报报道了新公交港湾将在历山路亮相的消息后&#xff0c;不少市…

2018展望| AI:巨头生态开始站队,深入垂直行业才能赚钱

来源&#xff1a;36氪“AI改变世界”这件事&#xff0c;在2018年会更值得人期待。不只是BAT&#xff0c;京东在谈智能仓储配送&#xff0c;滴滴在谈智慧交通……BAT&#xff0c;以及滴滴、京东这样的小巨头&#xff0c;手中攥着大量数据、也有直接服务消费者的场景&#xff0c;…

记录遇到的Python陷阱和注意点

来源&#xff1a;http://www.cnblogs.com/wilber2013/p/5178620.html 最近使用Python的过程中遇到了一些坑&#xff0c;例如用datetime.datetime.now()这个可变对象作为函数的默认参数&#xff0c;模块循环依赖等等。 在此记录一下&#xff0c;方便以后查询和补充。 避免可变…

归并排序(c++)

归并排序 归并排序&#xff08;Merge Sort&#xff09;是建立在归并操作上的一种有效&#xff0c;稳定的排序算法&#xff0c;该算法是采用分治法&#xff08;Divide and Conquer&#xff09;的一个非常典型的应用。将已有序的子序列合并&#xff0c;得到完全有序的序列&#x…