浙大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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值