zoukankan      html  css  js  c++  java
  • 剑指OFFER之重建二叉树

    题目描述:

    输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列。

     

    输入:

    输入可能包含多个测试样例,对于每个测试案例,

    输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数。

    输入的第二行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列。

    输入的第三行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的中序遍历序列。

    输出:

    对应每个测试案例,输出一行:

    如果题目中所给的前序和中序遍历序列能构成一棵二叉树,则输出n个整数,代表二叉树的后序遍历序列,每个元素后面都有空格。

    如果题目中所给的前序和中序遍历序列不能构成一棵二叉树,则输出”No”。

    样例输入:
    8
    1 2 4 7 3 5 6 8
    4 7 2 1 5 3 8 6
    8
    1 2 4 7 3 5 6 8
    4 1 2 7 5 3 8 6
    
    样例输出:
    7 4 2 5 8 6 3 1 
    No

    Code:
    #include <iostream>
     
    using namespace std;
     
    struct Node{
        int data;
        Node *lchild;
        Node *rchild;
    }Tree[1010];
     
    int loc;
    int str1[1010],str2[1010];
    bool canBuild;
     
    Node *create(){
        Tree[loc].lchild=Tree[loc].rchild=NULL;
        return &Tree[loc++];
    }
     
    int position(int s,int e,int key){
        for(int i=s;i<=e;++i){
            if(str2[i]==key){
                return i;
            }
        }
        return -1;
    }
     
    Node *build(int s1,int e1,int s2,int e2){
        Node *ret=create();
        ret->data=str1[s1];
        int index=position(s2,e2,str1[s1]);
        if(index==-1){
            canBuild=false;
            return NULL;
        }else{
            if(index!=s2){
                ret->lchild=build(s1+1,s1+index-s2,s2,index-1);
            }
            if(index!=e2){
                ret->rchild=build(s1+index-s2+1,e1,index+1,e2);
            }
        }
        return ret;
    }
     
    void postOrder(Node *T){
        if(T!=NULL){
            if(T->lchild!=NULL){
                postOrder(T->lchild);
            }
            if(T->rchild!=NULL){
                postOrder(T->rchild);
            }
            cout<<T->data<<" ";
        }
    }
     
    int main(){
        int n;
        while(cin>>n){
            loc=0;
            canBuild=true;
            for(int i=0;i<n;++i){
                cin>>str1[i];
            }
            for(int i=0;i<n;++i){
                cin>>str2[i];
            }
            Node *ret=NULL;
            ret=build(0,n-1,0,n-1);
            if(canBuild==true){
                postOrder(ret);
                cout<<endl;
            }else{
                cout<<"No"<<endl;
            }
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1385
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:10 ms
        Memory:1552 kb
    ****************************************************************/
  • 相关阅读:
    整理:分页存储过程整理
    净利润-流通市值比率”与公司估值
    常见7种股票底部形态(图解)
    nginx
    移动成本分布1
    浅谈公开信息检索和判断能力
    股票技术分析 成交量与换手率专题
    成份股和成份股指数
    股票底部形态初探
    筹码拉抬派发法
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4149169.html
Copyright © 2011-2022 走看看