PAT程序设计考题——甲级1043(Is It a Binary Search Tree ) C++实现
来源:互联网 发布:软件开发认证 编辑:程序博客网 时间:2024/06/05 15:04
点击打开pat链接
#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<vector>
using namespace std;
#define INF 100000000
#define maxn 100010
struct node{
int num;
node *lchild,*rchild;
};
node *newnode(int v){
node *root=new node;
root->num=v;
root->lchild=root->rchild=NULL;
return root;
}
void insert(node *&root,int v)
{
if(root==NULL)
{
root=newnode(v);
return;
}
// if(root->num==v) return;注意这个条件是不需要的
if(root->num>v)
insert(root->lchild,v);
// if(root->num<v)
else
insert(root->rchild,v);
}
int num;
node *lchild,*rchild;
};
node *newnode(int v){
node *root=new node;
root->num=v;
root->lchild=root->rchild=NULL;
return root;
}
void insert(node *&root,int v)
{
if(root==NULL)
{
root=newnode(v);
return;
}
// if(root->num==v) return;注意这个条件是不需要的
if(root->num>v)
insert(root->lchild,v);
// if(root->num<v)
else
insert(root->rchild,v);
}
int pre[maxn];
int post[maxn];
int m=0,n=0;
void pret(node *&root)
{
if(root==NULL) return;
pre[m++]=root->num;
pret(root->lchild);
pret(root->rchild);
}
int w=0;
void pret2(node *&root)
{
if(root==NULL) return;
pre[w++]=root->num;
pret2(root->rchild);
pret2(root->lchild);
int post[maxn];
int m=0,n=0;
void pret(node *&root)
{
if(root==NULL) return;
pre[m++]=root->num;
pret(root->lchild);
pret(root->rchild);
}
int w=0;
void pret2(node *&root)
{
if(root==NULL) return;
pre[w++]=root->num;
pret2(root->rchild);
pret2(root->lchild);
}
bool issame(int a[],int b[],int num)
{ for(int i=0;i<num;i++)
{
if(a[i]!=b[i]) return false;
}
return true;
}
void postt(node *root)
{ if(root==NULL) return;
postt(root->lchild);
postt(root->rchild);
post[n++]=root->num;
}
int q=0;
void postt2(node *root)
{ if(root==NULL) return;
postt2(root->rchild);
postt2(root->lchild);
post[q++]=root->num;
}
int main()
{ int num;
cin>>num;
node *root=NULL;//注意这边一定要为空否则会出错
int start[maxn];
for(int i=0;i<num;i++)
{
cin>>start[i];
insert(root,start[i]);
}
pret(root);
if(issame(start,pre,num))
{
cout<<"YES"<<endl;
postt(root);
for(int i=0;i<num;i++){
cout<<post[i];
if(i<num-1) cout<<" ";}
return 0;
}
pret2(root);
if(issame(start,pre,num))
{
cout<<"YES"<<endl;
postt2(root);
for(int i=0;i<num;i++){
cout<<post[i];
if(i<num-1) cout<<" ";}
}
else cout<<"NO"<<endl;
return 0;
}
bool issame(int a[],int b[],int num)
{ for(int i=0;i<num;i++)
{
if(a[i]!=b[i]) return false;
}
return true;
}
void postt(node *root)
{ if(root==NULL) return;
postt(root->lchild);
postt(root->rchild);
post[n++]=root->num;
}
int q=0;
void postt2(node *root)
{ if(root==NULL) return;
postt2(root->rchild);
postt2(root->lchild);
post[q++]=root->num;
}
int main()
{ int num;
cin>>num;
node *root=NULL;//注意这边一定要为空否则会出错
int start[maxn];
for(int i=0;i<num;i++)
{
cin>>start[i];
insert(root,start[i]);
}
pret(root);
if(issame(start,pre,num))
{
cout<<"YES"<<endl;
postt(root);
for(int i=0;i<num;i++){
cout<<post[i];
if(i<num-1) cout<<" ";}
return 0;
}
pret2(root);
if(issame(start,pre,num))
{
cout<<"YES"<<endl;
postt2(root);
for(int i=0;i<num;i++){
cout<<post[i];
if(i<num-1) cout<<" ";}
}
else cout<<"NO"<<endl;
return 0;
}
阅读全文
0 0
- PAT程序设计考题——甲级1043(Is It a Binary Search Tree ) C++实现
- PAT程序设计考题——甲级1102( Invert a Binary Tree ) C++实现
- 【PAT甲级】1043. Is It a Binary Search Tree (25)——JAVA实现
- 【PAT甲级】1043. Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25) PAT甲级
- PAT甲级1043. Is It a Binary Search Tree (25)
- PAT 甲级 1043. Is It a Binary Search Tree (25)
- PAT程序设计考题——甲级1065( A+B and C (64bit) ) C++实现
- 1043. Is It a Binary Search Tree (25)—PAT
- 1043. Is It a Binary Search Tree (25)-PAT甲级真题
- PAT甲级练习1043. Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)-PAT甲级真题
- PAT程序设计考题——甲级1005( Spell It Right ) C++实现
- PAT 1043 Is It a Binary Search Tree (25)
- PAT程序设计考题——甲级1066(Root of AVL Tree ) C++实现
- PAT程序设计考题——甲级1020( Tree traverse ) C++实现
- PAT程序设计考题——甲级1001( A+B Format ) C++实现
- PAT程序设计考题——甲级1002(A+B for Polynomials ) C++实现
- Redis数据库操作
- SQL Script
- eclipse 添加任务标记
- 0706 CF#822A-I'm bored with life #822B-Crossword solving
- LIS(最长递增子序列) Zigzag
- PAT程序设计考题——甲级1043(Is It a Binary Search Tree ) C++实现
- Mysql千万数据索引查询优化
- php 代码检测工具
- 利用makefile文件编译c++源文件
- 每日英语阅读(五)
- CopyOnWriteArrayList
- Python学习-课后作业2
- namenode安全模式问题
- Contest Score (优先队列)