zoukankan      html  css  js  c++  java
  • PAT 1138 Postorder Traversal [比较]

    1138 Postorder Traversal (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the corresponding binary tree.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 50,000), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in one line the first number of the postorder traversal sequence of the corresponding binary tree.

    Sample Input:

    7
    1 2 3 4 5 6 7
    2 3 1 5 4 7 6
    

    Sample Output:

    3

     题目大意:给出二叉树的前序和中序,输出后序遍历的第一个节点。

    //这道题看似简单,但是容易超时。

    #include <iostream>
    #include <vector>
    #include<cstdio>
    using namespace std;
    
    vector<int> pre,in,post;
    void getPost(int inL,int inR,int preL,int preR){
        if(inL>inR||post.size()!=0)return ;
        //if(preL>preR)return ;
        int i=0;
        while(in[i]!=pre[preL])i++;
        getPost(inL,i-1,preL+1,preL+i-inL);
        getPost(i+1,inR,preL+i-inL+1,preR);
        post.push_back(pre[preL]);
    }
    int main() {
        int n;
        cin>>n;
        int t;
        for(int i=0;i<n;i++){
            cin>>t;
            pre.push_back(t);
        }
        for(int i=0;i<n;i++){
            cin>>t;
            in.push_back(t);
        }
        getPost(0,n-1,0,n-1);
        cout<<post[0];
        return 0;
    }

    //这样写取判断总有最后两个或者倒数第二个测试点过不去,运行超时。

    //尝试想传递&引用,发现不行,报错:

    main.cpp|12|error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'|

    改成以下之后,也就是将函数参数减少一个:

    #include <iostream>
    #include <vector>
    #include<cstdio>
    using namespace std;
    
    vector<int> pre,in,post;
    void getPost(int inL,int inR,int pr){
        if(inL>inR||post.size()>0)return ;
        //if(preL>preR)return ;
        int i=0;
        while(in[i]!=pre[pr])i++;
        getPost(inL,i-1,pr+1);
        getPost(i+1,inR,pr+i-inL+1);
        post.push_back(pre[pr]);
    }
    int main() {
        int n;
        cin>>n;
        int t;
        for(int i=0;i<n;i++){
            cin>>t;
            pre.push_back(t);
        }
        for(int i=0;i<n;i++){
            cin>>t;
            in.push_back(t);
        }
        getPost(0,n-1,0);
        cout<<post[0];
        return 0;
    }

    倒数第二个测试点超时。

    //忽然想起,将i初始化时改为inL,然后就AC了!!!这样遍历的就少了。

    主要问题就是i的初始化问题,一定要初始化为inL,修改之后第一个代码也过了,说明运行超时不是函数传参参数个数的问题。

  • 相关阅读:
    小贝_mysql 存储过程
    Codeforces Round #253 (Div. 1)-A,B
    rac环境改动spfile后遭遇ora-29250小例
    Linux学习笔记——例说makefile 索引博文
    《信息检索》课程论文撰写指南 及 分享加分说明
    git mirror的创建与使用
    一起talk GDB吧(第二回:GDB单步调试)
    nginx源代码分析--配置信息的继承&amp;合并
    EasyUI基础入门之Droppable(可投掷)
    自己动手写CPU之第七阶段(5)——流水线暂停机制的设计与实现
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9984670.html
Copyright © 2011-2022 走看看