hdu1710-BinaryTreeTraversals

  1. Binary Tree Traversals

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2808    Accepted Submission(s): 1245


    Problem Description
    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
     

    Source

對於整數的,根據前序和中序構建二叉樹;

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1005;
struct node{
    int data;
    node *lchild;
    node *rchild;
};

node *creatTree(int *pre, int *in, int len){
    node *root=new node;
    root->data=pre[0];
    root->lchild=root->rchild=NULL;
    int subTreeLen = 0;
    while(subTreeLen<len && in[subTreeLen]!=pre[0]) subTreeLen++;//找到pre[0]對應的中序的位置
    if(subTreeLen>0) 
        root->lchild=creatTree(pre+1,in,subTreeLen);
    if(len-1-subTreeLen>0) 
        root->rchild=creatTree(pre+1+subTreeLen,in+subTreeLen+1,len-subTreeLen-1);
    return root;
}

queue<node*> Q;
void preorder(node *root){
    if(root!=NULL){
        preorder(root->lchild);
        preorder(root->rchild);
        Q.push(root);
    }
}

int main()
{
    node *root;
    int num_1[MAXN],num_2[MAXN],n;
    while(cin>>n && n){
        for(int i=0; i<n; i++) cin>>num_1[i];
        for(int i=0; i<n; i++) cin>>num_2[i];
        root = creatTree(num_1,num_2,n);
        preorder(root);
        node *pre=Q.front();
        Q.pop();
        cout<<pre->data;
        while(!Q.empty()){
            pre = Q.front();
            Q.pop();
            cout<<" "<<pre->data;
        }
        cout<<endl;
        root=NULL;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章