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"; }

最新文章

  1. 今天遇到的点击添加按钮button_click代码段无法执行的问题
  2. (C语言)数组与指针的区别
  3. Bounce.js – 快速创建漂亮的 CSS3 动画效果
  4. python grammar、C/C++ Python Parsing Engine
  5. fastqc, Per Base Sequence Content
  6. Ioc正解
  7. 读文档readarx.chm
  8. Java经典面试题
  9. Objective-C中的@dynamic(转)
  10. 在X64系统中PowerDesigner无法连接MySQL的解决方法
  11. JAVAscript学习笔记 js句柄监听事件 第四节 (原创) 参考js使用表
  12. 在电脑上安装Linux操作系统
  13. mahout系列----minhash聚类
  14. Bootstrap免费模板站推荐
  15. Vue 组件&amp;组件之间的通信 父子组件的通信
  16. Java链接MySQL数据库的配置文件
  17. c++中static变量有什么用
  18. node+express+mongodb初体验
  19. 2018/05/02 每日一学Linux 之 .bash_profile和.bashrc的区别
  20. SolrServer SolrRequest

热门文章

  1. hadoop HDFS扩容
  2. TARS基金会:构建微服务开源生态
  3. HTML5&amp;CCS3(1) 网页的构造块
  4. JAVAEE学习day01
  5. python基础学习day7
  6. (转)const的内部链接属性(C++中适用)
  7. Spring注解 - AOP 面向切面编程
  8. jvm 性能调优工具之 jstat 命令详解
  9. MyBatis框架——多表查询
  10. python 清空list的几种方法