PAT 1119. Pre- and Post-order Traversals (30)

#include
#include
#include
#include
using namespace std;
int n,pre[35],post[35],flag=0;
int ans[35],k;
struct node{
  int num;
  struct node* left;
  struct node* right;
};
struct node* build(struct node* t,int preleft,int preright,int postleft,int postright){
//	cout<n||postleft<0||preright>n||preleft<0) return t;
  
  if(t==NULL){
    struct node* p=(struct node*)malloc(sizeof(struct node));
    if(p==NULL) return p;
    p->num=pre[preleft];
    p->left=NULL;
    p->right=NULL;
    t=p;
   // free(p);
  }
  if(preleftleft=build(t->left,preleft+1,preleft+len,postleft,i);
    len=postright-(i+1);
    t->right=build(t->right,preright-len+1,preright,i+1,postright-1);
  }
  return t;
}
int level(struct node* t){
//  cout<left!=NULL&&t->right==NULL)||(t->left==NULL&&t->right!=NULL)){
//    cout<<"No"<left!=NULL){
    level(t->left);
  }
//  cout<num<<' ';
  ans[k++]=t->num;
  if(t->right!=NULL){
    level(t->right);
  }
  return 0;
}
int main(){
  int n;
  flag=0;
  cin>>n;
  for(int i=0;i>pre[i];
  }
  for(int i=0;i>post[i];
  }
  struct node* t;
  t=NULL;
  t=build(t,0,n-1,0,n-1);
  level(t);
  if(flag){
  	cout<<"No"<

你可能感兴趣的:(PAT,树)