博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
阅读量:6254 次
发布时间:2019-06-22

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

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:
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
AC代码:二叉树的重建与遍历。
#include
#include
#define MAX 1005using 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
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
left) PostOrderTraverse(root->left); if(root->right) PostOrderTraverse(root->right); PostOrder[index++]=root->data;}void Display(int len){ int i; for(i=0;i
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

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

你可能感兴趣的文章
javascript基础篇:函数
查看>>
SVN与TortoiseSVN实战:补丁详解
查看>>
java一些面试题
查看>>
干货型up主
查看>>
获取页面中所有dropdownlist类型控件
查看>>
读《淘宝数据魔方技术架构解析》有感
查看>>
Chrome 报 Resource interpreted as Script but transferred with MIME type text/plain 警告的解决办法...
查看>>
memcpy的使用方法总结
查看>>
[转载]如何破解Excel VBA密码
查看>>
手机web——自适应网页设计(html/css控制) - 51CTO.COM
查看>>
【BZOJ】2563: 阿狸和桃子的游戏
查看>>
redis 中文字符显示
查看>>
webview与JS的交互
查看>>
国内外MD5在线解密网站
查看>>
【OC语法要闻速览】一、方法调用
查看>>
使用光标
查看>>
php生成curl命令行
查看>>
Android NumberPicker 修改分割线颜色和高度及字体颜色大小
查看>>
树莓派键盘布局修正
查看>>
常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服...
查看>>