栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

hdu 1710 Binary Tree Traversals

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

hdu 1710 Binary Tree Traversals

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4438 Accepted Submission(s): 2025

Problem Description

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

Input

The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.

Output

For each test case print a single line specifying the corresponding postorder sequence.

Sample Input

9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

Sample Output

7 4 2 8 9 5 6 3 1

Submit
#include 
using namespace std;
const int N = 1010;
int pre[N], in[N], post[N]; // 先序,中序,后序
int k;
struct node
{
    int value;
    node *l, *r;
    node(int value = 0, node *l = NULL, node *r = NULL) : value(value), l(l), r(r) {}
};
void buildtree(int l, int r, int &t, node *&root) //建树
{
    int flag = -1;
    for (int i = l; i <= r; i++) //先序的第一个数是根,找到对应的中序位置
    {
        if (in[i] == pre[t])
        {
            flag = i;
            break;
        }
    }
    if (flag == -1) //结束
        return;
    root = new node(in[flag]); //新建结点
    t++;
    if (flag > l)
        buildtree(l, flag - 1, t, root->l);
    if (flag < r)
        buildtree(flag + 1, r, t, root->r);
}
void preorder(node *root)
{
    if (root != NULL)
    {
        post[k++] = root->value;
        preorder(root->l);
        preorder(root->r);
    }
}
void inorder(node *root)
{
    if (root != NULL)
    {
        inorder(root->l);
        post[k++] = root->value;
        inorder(root->r);
    }
}
void postorder(node *root)
{
    if (root != NULL)
    {
        postorder(root->l);
        postorder(root->r);
        post[k++] = root->value;
    }
}
void remove_tree(node *root) //释放空间
{
    if (root == NULL)
        return;
    remove_tree(root->l);
    remove_tree(root->r);
    delete root;
}
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        for (int i = 1; i <= n; i++)
            scanf("%d", &pre[i]);
        for (int i = 1; i <= n; i++)
            scanf("%d", &in[i]);
        node *root;
        int t = 1;
        buildtree(1, n, t, root);
        k = 0; // 记录结点个数
        postorder(root);
        for (int i = 0; i < k; i++)
            printf("%d %c", post[i], i == k - 1 ? 'n' : ' ');
        remove_tree(root);
    }
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/683797.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号