博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1043. Is It a Binary Search Tree (25)
阅读量:4072 次
发布时间:2019-05-25

本文共 3070 字,大约阅读时间需要 10 分钟。

1043. Is It a Binary Search Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
78 6 5 7 10 8 11
Sample Output 1:
YES5 7 6 8 11 10 8
Sample Input 2:
78 10 11 8 6 7 5
Sample Output 2:
YES11 8 10 7 5 6 8
Sample Input 3:
78 6 8 5 10 9 11
Sample Output 3:
NO
 
#include 
#include
#include
#include
#include
#include
#include
using namespace std;struct Node{ int data; Node * left; Node * right; Node():data(0),left(NULL),right(NULL) {}; Node(int x):data(x),left(NULL),right(NULL) {};};void Post(Node *T,vector
* o){ if(!T) return ; if(T->left) Post(T->left,o); if(T->right) Post(T->right,o); (*o).push_back(T->data);}vector
nums;Node *bstCreate(int sstart,int eend,bool *flag){ if(sstart>eend) return NULL; else { Node *T = new Node(nums[sstart]); int i = sstart+1; while(i<=eend&&nums[i]
=nums[sstart]) j++; if(i!=eend+1&&j!=eend+1) { *flag = false; return NULL; } T->left = bstCreate(sstart+1,i-1,flag); T->right = bstCreate(i,eend,flag); return T; }}Node *MirrorCreate(int sstart,int eend,bool *flag){ if(sstart>eend) return NULL; else { Node *T = new Node(nums[sstart]); int i = sstart+1; while(i<=eend&&nums[i]>=nums[sstart]) i++; int j = i; while(j<=eend&&nums[j]
left = MirrorCreate(sstart+1,i-1,flag); T->right = MirrorCreate(i,eend,flag); return T; }}int main(){ int n,temp; Node *root=NULL; scanf("%d",&n); for(int i=0; i
o; bool Success=true; root = bstCreate(0,nums.size()-1,&Success); if(Success) Post(root,&o); else { Success = true; root = MirrorCreate(0,nums.size()-1,&Success); if(Success) Post(root,&o); } if(!Success) printf("NO\n"); else { printf("YES\n"); for_each(o.begin(),o.end(),[&](int a) { static int countt=0; printf("%d",a); countt++; printf(countt==o.size()?"\n":" "); }); } return 0;}//8 6 8 5 10 9 11

转载地址:http://xmhji.baihongyu.com/

你可能感兴趣的文章
最近用C#写Winform的一个心得
查看>>
PHP中日期相加减
查看>>
Ext中RowExpander数据刷新
查看>>
Ext中tabpanel对面板的添加
查看>>
Ext中的选择器
查看>>
自己设计的一个PHP的MVC framework
查看>>
ext中联动combo远程加载选中的解决
查看>>
ie下对于window.location.href的跳转时获取不到referer的,php中的路径包含有未定式的
查看>>
一段有用的jquery代码
查看>>
c#中队trunked的处理
查看>>
笔记本无线路由组件的局域网ping不通的问题
查看>>
php中require后的路径问题
查看>>
ext直接导出结果到excel
查看>>
combotree的总结
查看>>
最近小结
查看>>
大规模图像分类器的演化-Large-Scale Evolution of Image Classifiers-读后杂谈
查看>>
java对大文件的处理思路
查看>>
qt中的一个问题
查看>>
MFC编写的人民币大小写转换
查看>>
error C3861: “mciSendString”: 找不到标识符 的解决
查看>>