二差树的三种队列-Binary Tree Traversals

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

    Input
    The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree. 



    Output
    For each test case print a single line specifying the corresponding postorder sequence. 



    Sample Input
    9

1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

    Sample Output
    7 4 2 8 9 5 6 3 1

题目大意:输入二叉树的先序和中序序列,求后续序列

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int pre[N],in[N],post[N];//定义先序,中序,后序三个数组 
int k;
struct node{
 int value;
 node *l,*r;
 node(int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
};//用指针来实现二叉树 
void buildtree(int l,int r,int &t,node *&root){
 int flag=-1;
 for(int i=1;i<=r;i++)
   if(in[i]==pre[t]){//在中序排列中找到树根 
    flag=i;
    break;
   }
   if(flag==-1)   return;
   root=new node(in[flag]);//新建一个node 
   t++;
   if(flag>l)    buildtree(l,flag-1,t,root->l);//递归建树 
   if(flag<r)    buildtree(flag+1,r,t,root->r);
}//建树 
void preorder(node *root){
 if(root!=NULL){//当树根的指针不为空时 
  post[k++]=root->value;//存储 
  preorder(root->l);
  preorder(root->r);
 }
}//求先序队列 
void inorder(node *root){
 if(root!=NULL){
  inorder(root->l);
  post[k++]=root->value;
  inorder(root->r);
 }
}//求中序队列 
void postorder(node *root){
 if(root!=NULL){
  postorder(root->l);
  postorder(root->r);
  post[k++]=root->value;
 }
}//求后序队列 
void remove_tree(node *root){
 if(root==NULL)   return ;
 remove_tree(root->l);
 remove_tree(root->r);
 delete root;
}//释放内存 
int main(){
 int n;
 while(~scanf("%d",&n)){
  for(int i=1;i<=n;i++)   scanf("%d",&pre[i]);
  for(int j=1;j<=n;j++)   scanf("%d",&in[j]);
  node *root;
  int  t=1;
  buildtree(1,n,t,root);//建树 
  k=0;
  postorder(root);
  for(int i=0;i<k;i++)    printf("%d%c",post[i],i==k-1?'\n':' ');
  remove_tree(root);
 }
 return 0;
} 
发布了29 篇原创文章 · 获赞 24 · 访问量 3682

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/104268715
今日推荐