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)的更多相关文章

  1. PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  2. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  3. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  4. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  5. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  6. 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 ...

  7. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  8. 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) ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. 一种基于Qt的可伸缩的全异步C/S架构服务器实现(流浪小狗,六篇,附下载地址)

    本文向大家介绍一种基于Qt的伸缩TCP服务实现.该实现针对C/S客户端-服务集群应用需求而搭建.连接监听.数据传输.数据处理均在独立的线程池中进行,根据特定任务不同,可安排负责监听.传输.处理的线程数 ...

  2. Delphi SysErrorMessage 函数和系统错误信息表

    在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMes ...

  3. C语言的本质(10)——指针本质

    指针,大概是C语言中最难理解的概念之一了.指针这个东西是C语言中的一个基本概念,C99中对于指针的定义是: 1. 指针的类型是derived from其它类型,也就是说指针的类型是由它指向的类型决定的 ...

  4. 一步一步实现AS3拖放组件

    外话: 我之前在天地会上发布过一个拖放组件,http://bbs.9ria.com/thread-117535-1-1.html 应该有人看过吧,那时候年纪轻轻,写了个东西,那时候基本能满足需求 但是 ...

  5. MAX Average Problem(斜率优化dp)

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. Wormholes(SPFA+Bellman)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36860   Accepted: 13505 Descr ...

  7. 嵌入式OS入门笔记-以RTX为案例:六.RTX的任务调度

    嵌入式OS入门笔记-以RTX为案例:六.RTX的任务调度 上一篇笔记介绍了一些绕开排程器(或调度程序,scheduler)来进行时间管理的一些小方法.这一篇详细介绍RTX的任务调度原理. RTX主要有 ...

  8. android开发之Animations的使用(二)

    android开发之Animations的使用(二) 本博文主要讲述的是android开发中的animation动画效果的使用,和上一篇博文不同的是,此次四种动画效果,主要使用的是xml文件实现的,提 ...

  9. Afinal开源框架中FinalActivity的使用

    1. 首先将afinal.jar文件复制到项目中的libs文件夹下 2. 让MainActivity不在继承系统的Activity,而是继承FinalActivity public class Mai ...

  10. UVALive 3635 Pie 切糕大师 二分

    题意:为每个小伙伴切糕,要求每个小盆友(包括你自己)分得的pie一样大,但是每个人只能分得一份pie,不能拿两份凑一起的. 做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又 ...