Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
Pat1043代码
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
AC代码:二叉树的重建与遍历。
#include<cstdio>
#include<cstdlib>
#define MAX 1005 using namespace std; typedef struct Node
{
int data;
Node *left;
Node *right;
}Node; int Keys[MAX];
int PreOrder[MAX];
int PreOrderImg[MAX];
int PostOrder[MAX];
int index=0; void InsertNode(Node **root,int key,bool Img)
{
if(*root==NULL)
{
*root=(Node *)malloc(sizeof(Node));
if(!(*root))
{
printf("No enough memory!\n");
exit(-1);
}
(*root)->left=NULL;
(*root)->right=NULL;
(*root)->data=key;
return;
}
else
{
if(Img)
{
if((*root)->data<=key)
InsertNode(&((*root)->left),key,Img);
else
InsertNode(&((*root)->right),key,Img);
}
else
{
if((*root)->data>key)
InsertNode(&((*root)->left),key,Img);
else
InsertNode(&((*root)->right),key,Img);
}
}
} void CreateBSTree(Node **root,int keys[],int len,bool Img)
{
for(int i=0;i<len;i++)
InsertNode(root,keys[i],Img);
} void PreOrderTraverse(Node *root,bool Img)
{
if(root==NULL)
return ;
if(Img)
PreOrderImg[index++]=root->data;
else
PreOrder[index++]=root->data;
if(root->left)
PreOrderTraverse(root->left,Img);
if(root->right)
PreOrderTraverse(root->right,Img);
} bool IsSame(int keys[],int len,bool Img)
{
int i;
if(Img)
{
for(i=0;i<len;i++)
{
if(keys[i]!=PreOrderImg[i])
return false;
}
return true;
}
else
{
for(i=0;i<len;i++)
{
if(keys[i]!=PreOrder[i])
return false;
}
return true;
}
} void PostOrderTraverse(Node *root)
{
if(root==NULL)
return;
if(root->left)
PostOrderTraverse(root->left);
if(root->right)
PostOrderTraverse(root->right);
PostOrder[index++]=root->data;
} void Display(int len)
{
int i;
for(i=0;i<len;i++)
{
if(i==0)
printf("%d",PostOrder[i]);
else
printf(" %d",PostOrder[i]);
}
printf("\n");
} void Destroy(Node **root)
{
if((*root)->left==NULL&&(*root)->right==NULL)
{
free(*root);
*root=NULL;
}
else if((*root)->left)
Destroy(&((*root)->left));
else if((*root)->right)
Destroy(&((*root)->right));
} int main(int argc,char *argv[])
{
int i,n;
Node *root=NULL;//这两个初始化必须得有,否则会出现段错误。在自己的机器
Node *rootImg=NULL; //上能够执行,但提交后就段错误,预计是server端
scanf("%d",&n); //编译器的问题吧
for(i=0;i<n;i++)
scanf("%d",&Keys[i]);
CreateBSTree(&root,Keys,n,false);
CreateBSTree(&rootImg,Keys,n,true);
index=0;
PreOrderTraverse(root,false);
if(IsSame(Keys,n,false))
{
printf("YES\n");
index=0;
PostOrderTraverse(root);
Display(n);
}
else
{
index=0;
PreOrderTraverse(rootImg,true);
if(IsSame(Keys,n,true))
{
printf("YES\n");
index=0;
PostOrderTraverse(rootImg);
Display(n);
}
else
{
printf("NO\n");
}
}
Destroy(&root);
Destroy(&rootImg); return 0;
}
Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)的更多相关文章
- PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
- PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...
随机推荐
- [Django] Pinax 项目下APP的 安装与使用
Pinax下有数十个APP,怎么将这些APP集成到已有的Django 工程(http://www.cnblogs.com/xiaoqu/p/3196081.html)文件中去呢?现在用django-u ...
- Keil MDK下如何设置非零初始化变量
一些工控产品,当系统复位后(非上电复位),可能要求保持住复位前RAM中的数据,用来快速恢复现场,或者不至于因瞬间复位而重启现场设备.而keil mdk在默认情况下,任何形式的复位都会将RAM区的非初始 ...
- Ubuntu 12.04下安装ibus中文输入法
这是最完整的安装方法: ibu是一个框架,可以支持多种输入法,像是pinyin,五笔等. 1,安装ibus框架 终端输入以下命令: sudo apt-get install ibus ibus-clu ...
- GCDAsyncUdpSocket的使用
tips: 要注意服务器端口与客户端端口的区别,如果客户端绑定的是服务器的端口,那么服务器发送的消息就会一直发送给服务器.
- skiing(搜索+记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- js正则验证两位小数 验证数字最简单正则表达式大全
<h3>输入完按回车后即可验证!(自认为最简单!)</h3> 正整数: <input type="text" size="20&quo ...
- C#中关于DBNULL的处理方法
从数据库中获取数据有些会是空值的,这时一不注意就会被坑了…… String.Concat(db.可能为DBNULL的值) 在这种情况下,如果是DBNULL,得到的会是""
- 前端入门HTML5扩展知识(一)
一. 请描述一个网页从开始请求到最终显示的完整过程? 1. 在浏览器中输入网址: 2. 发送至 DNS 服务器并获得域名对应的 WEB 服务器的 IP 地址: 3. 与 WEB 服务器建立 TC ...
- Ubuntu Android Studio 无法通过起动器开启
问题: 1.可以通过终端开启 2.通过Android-Studio建立的Application无法启动, 提示 No JDK found. Please validate either STUDIO_ ...
- Spring IOC的描述和Spring的注解(转)
Spring常用的注解 本文系转载:转载网址: http://www.cnblogs.com/xdp-gacl/p/3495887.html http://ljhzzyx.blog.163.com/b ...