hdu1710(二叉树的历遍)

/*
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4644    Accepted Submission(s): 2113

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

Source
HDU 2007-Spring Programming Contest

Recommend
lcy   |   We have carefully selected several similar problems for you:  1711 1708 1707 1709 1509
*/
#include <iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
const int Nmax=1001;
int pre[Nmax];
int in[Nmax];
int post[Nmax];
struct BTree
{
    int data;
    BTree *l;
    BTree *r;
}*p;
BTree* creatBT(int ls,int rs,int pos)
{
    BTree *p = new BTree;
    if(ls==rs)
        return NULL;
    for(int i=ls; i<rs; i++)
    {
        if(in[i]==pre[pos])
        {
            p->data=pre[pos];
            p->l=creatBT(ls,i,pos+1);
            p->r=creatBT(i+1,rs,pos+1+i-ls);
        }
    }
    return p;
}
void postTra(BTree* pt)
{
    if(pt==NULL)
        return;
    postTra(pt->l);
    postTra(pt->r);
    if(pt==p)
        printf("%d\n",pt->data);
    else
        printf("%d ",pt->data);
}
int main()
{
    int i,n;
    while(scanf("%d",&n)!=EOF)
    {
        p=NULL;
        for(i=0; i<n; i++)
            scanf("%d",&pre[i]);
        for(i=0; i<n; i++)
            scanf("%d",&in[i]);
        p=creatBT(0,n,0);
        postTra(p);
    }
    return 0;
}
时间: 2024-11-09 03:44:00

hdu1710(二叉树的历遍)的相关文章

jquery元素的历遍

今天遇到的问题是,jQuery元素的历遍要使用jquery自己的each函数 例如 $('.img_big').each(function(){ $(this).text() }) 如果用js的for循环历遍,元素就得使用js原生的方法进行操作

PHP之历遍数组问题(while迭代list()与each()历遍数组原理)

今天学习数组的时候遇到了一个让我头疼了很久的问题,当然,对于像我这样刚刚学习的人或许会有或多或少的帮助. 首先,我定义了一个二维数组,如下: $product=array( array('Code'=>'TIRE','Description'=>'Tires','Price'=>100), array('Code'=>'OIL','Description'=>'Oil','Price'=>10), array('Code'=>'SPARK','Descriptio

Python 历遍目录

Automate the Boring Stuff 学习笔记 01 使用 os 模块的 walk() 函数可以实现历遍目录的操作,该函数接收一个绝对路径字符串作为必选参数,返回三个参数: 当前目录——指程序当前工作目录——名称(字符串格式) 当前目录——指程序当前所历遍到的目录——下所有子文件夹(列表格式) 当前目录——指程序当前所历遍到的目录——下所有文件(列表格式) 假设有如下文件结构: 程序代码如下: import os for folderName, subfolders, filena

uva536_树的重建(由先序,中序历遍推出后续历遍)

/////////////////////////////////////////////////////////////////////////////////////////////////////// 作者:tt2767 声明:本文遵循以下协议自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 查看本文更新与讨论请点击:http://blog.csdn.net/tt2767 链接被删请百度: CSDN tt2767 ///////////////

C# 历遍对象属性

今天有个网友问如何历遍对象的所有公共属性,并且生成XML.采用序列化方式的话比较简单,我写个手工解析的例子,这样能让初学者更加理解也比较灵活,记录一下吧或许会有人用到. 对象模型: public class Master { public string Description { get; set; } public List<Slave> Slaves { get; set; } } public class Slave { public int ID { get; set; } publi

前中后序建立树或者直接历遍

前中后序建立树或者直接历遍 代码实现 void postOrder(int root,int start,int end) { if (start > end) return; int index = start; while (inOrder[index] != preOrder[root] ) index++; postOrder(root + 1, start, index - 1); postOrder(root + index - start + 1, index + 1, end);

uva122_二叉树的层次历遍

大白2,P150页 #include<cstdio> #include<iostream> #include<cstring> #include<vector> #include<queue> const int N = 100009; struct Node { bool have_value; int v; Node *left,*right; Node(): have_value(false),left(NULL),right(NULL)

HDU1710 二叉树的前、中、后遍历

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 consi

二叉树的层次遍历法

void print_by_level_2(treeNode* Root) { if(! Root) return; deque<treeNode*> dequeTreeRoot; dequeTreeRoot.push_back(Root); while(!dequeTreeRoot.empty()) { treeNode *temp = dequeTreeRoot.front(); dequeTreeRoot.pop_front(); cout << temp->data