浙大pat甲级题目---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 (<=30), 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

题目大意:已知一棵树的后序遍历(postorder)和中序遍历(inoder),求这棵树按照层输出的结果(bfs输出)

题目思路:

首先先说明这道题我是网上搜了题解的,代码参考了

http://blog.csdn.net/xyt8023y/article/details/46273967

这篇博客,文章里思路介绍的很清楚,大家可以转到这片博客。下面站在我的角度说一说这道题。

首先这是一道树的题,而且是二叉树,二叉树的题最核心最核心的思想我觉得就是递归,所以不难想到这道题一定会用到递归。

怎么用呢?我们知道,针对每一个子树来说后序遍历的最末尾就是该树的根,再通过这个数字在中序遍历中的位置,可以将这棵子树再划分为左右两个子树。

那么程序怎么实现呢?首先递归一定要有参数,所以就以对于中序遍历来说左右两个子树的端点来做参数,同时需要一个current变量全局变量来作为当前子树的根的后序遍历索引。

当构建出来树之后,就是对树bfs遍历即可(建议背住):

#include <iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int>postorder;
vector<int>inorder;
int current;
struct tree
{
    tree* left;
    tree* right;
    int root;
};
int get_index(int c)
{
    for(int i=0;i<(int)inorder.size();i++)
    {
        if(c==inorder[i])
            return i;
    }
    return -1;
}
tree* build(int left,int right)
{
    if(left>right)//递归终止条件
        return NULL;
    tree* node=(tree*)malloc(sizeof(struct tree));//给节点分配空间
    int curRoot=postorder[current];
    current--;
    int index=get_index(curRoot);//获取当前节点在中序遍历的索引
    node->root=curRoot;
    if(right==left)//叶子节点
    {
        node->right=NULL;
        node->left=NULL;
    }
    else
    {
        node->right=build(index+1,right);
        node->left=build(left,index-1);//注意顺序
    }
    return node;
}
void bfs(tree *t)
{
    bool first=true;
    queue<tree*>q;
     q.push(t);
    while(!q.empty())
    {
        tree* temp=q.front();
        q.pop();
        if(first)
        {
            printf("%d",temp->root);
            first= false;
        }
        else
            printf(" %d",temp->root);

        if(temp->left!=NULL)
        {
            q.push(temp->left);
        }
        if(temp->right!=NULL)
        {
            q.push(temp->right);
        }
    }
}
int main() {
    int n;
    postorder.resize(n);
    inorder.resize(n);
    scanf("%d",&n);
    current=n-1;
    for(int i=0;i<n;i++)
    {
        int temp;
        scanf("%d",&temp);
        postorder.push_back(temp);
    }
    for(int i=0;i<n;i++)
    {
        int temp;
        scanf("%d",&temp);
        inorder.push_back(temp);
    }
    tree* t=build(0,n-1);
    bfs(t);
    return 0;
}

原文地址:https://www.cnblogs.com/SK1997/p/8570126.html

时间: 2024-10-20 20:38:47

浙大pat甲级题目---1020. Tree Traversals (25)的相关文章

PAT (Advanced Level) 1020. Tree Traversals (25)

递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=40; int a[maxn],b[maxn]; int n,tot; struc

PAT 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.

【PAT】1020 Tree Traversals (25)(25 分)

1020 Tree Traversals (25)(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

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.

1020. Tree Traversals (25) PAT甲级真题

之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后续和中序,先推出整个二叉树,再考虑 对二叉树层次遍历. 本题还有一点要注意的时在输出结果的末尾,如果使用了类似 pirntf("%d ",data); 这样的格式是不对的,一定要对末尾进行判断消除最尾端的空格. 首先最核心的部分是通过两次遍历反推回二叉树:这里的思路是,后续遍历的最末尾,一

PAT:1020. Tree Traversals (25) AC

#include<stdio.h> #include<stdlib.h> #include<queue> using namespace std; int POST[32]; //存放后序遍历 int IN[32]; //存放中序遍历 int n; //节点数 struct node { int data; node* l; node* r; }; node* creat(int postL,int postR,int inL,int inR) { if(postL&g

1020. Tree Traversals (25)

根据两种遍历方式建立二叉树 层遍历二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 le

1020. Tree Traversals (25) -BFS

题目如下: 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:

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