zoukankan      html  css  js  c++  java
  • PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)
     

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order 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 (≤), the total number of nodes in the binary tree. The second line gives the postorder 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 level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

    Sample Input:

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

    Sample Output:

    4 1 6 3 5 7 2

    主要考查,中后遍历转前序,前中遍历建树,层序遍历树
    #include <iostream>
    #include <vector>
    #include <queue>
    
    using namespace std;
    
    struct TreeNode{
        int val;
        TreeNode *left;
        TreeNode *right;
    };
    
    vector<int> pre,in,post,ans;
    queue<TreeNode*> que;
    
    void preOrder(int root,int start,int end){
        if(start>end) return;
        int i=0;
        while(i<=end&&in[i]!=post[root]) i++;
        pre.push_back(post[root]);
        preOrder(root-1-end+i,start,i-1);
        preOrder(root-1,i+1,end);
    }
    
    TreeNode* buildTree(int root,int start,int end){
        if(start>end) return NULL;
        int i=0;
        TreeNode *t=new TreeNode();
        while(i<=end&&in[i]!=pre[root]) i++;
        t->val=pre[root];
        t->left=buildTree(root+1,start,i-1);
        t->right=buildTree(root+1+i-start,i+1,end);
        return t;
    }
    
    void levelOrder(TreeNode *tree){
        que.push(tree);
        while(!que.empty()){
            TreeNode *tmp=que.front();
            ans.push_back(tmp->val);
            que.pop();
            if(tmp->left!=NULL) que.push(tmp->left);
            if(tmp->right!=NULL) que.push(tmp->right);
        }
    
    }
    int main()
    {
        int N;
        scanf("%d",&N);
        post.resize(N);in.resize(N);
        for(int i=0;i<N;i++) scanf("%d",&post[i]);
        for(int i=0;i<N;i++) scanf("%d",&in[i]);
        preOrder(N-1,0,N-1);
        TreeNode *tree=buildTree(0,0,N-1);
        levelOrder(tree);
        for(int i=0;i<ans.size();i++)
            if(i!=ans.size()-1) cout<<ans[i]<<" ";
            else cout<<ans[i];
        system("pause");
        return 0;
    }

    查看柳神博客,对代码修改,实际可以建立一个索引,这样就可以直接得到先序遍历的结果:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    struct node{
        int index;
        int val;
    };
    bool cmp(node n1,node n2){
        return n1.index<n2.index;
    }
    vector<int> in,post;
    vector<node> ans;
    
    void preOrder(int root,int start,int end,int index){
        if(start>end) return;
        int i=0;
        while(i<=end&&in[i]!=post[root]) i++;
        ans.push_back({index,post[root]});
        preOrder(root-1-end+i,start,i-1,2*index+1);
        preOrder(root-1,i+1,end,2*index+2);
    }
    int main()
    {
        int N;
        scanf("%d",&N);
        post.resize(N);in.resize(N);
        for(int i=0;i<N;i++) scanf("%d",&post[i]);
        for(int i=0;i<N;i++) scanf("%d",&in[i]);
        preOrder(N-1,0,N-1,0);
        sort(ans.begin(),ans.end(),cmp);
        for(int i=0;i<N;i++)
            if(i!=N-1) printf("%d ",ans[i].val);
            else printf("%d",ans[i].val);
        system("pause");
        return 0;
    }
  • 相关阅读:
    SQLServer之创建表值函数
    SQLServer之创建标量函数
    SQLServer之函数简介
    SQLServer之创建分布式事务
    SQLServer之创建隐式事务
    SQLServer之创建显式事务
    SQLServer之事务简介
    SQLServer之删除存储过程
    geoserver 添加图层数据
    geoserver入门
  • 原文地址:https://www.cnblogs.com/littlepage/p/11672360.html
Copyright © 2011-2022 走看看