博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树 - 已知前中,求后序遍历
阅读量:5854 次
发布时间:2019-06-19

本文共 2758 字,大约阅读时间需要 9 分钟。

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.
InputThe 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.
OutputFor each test case print a single line specifying the corresponding postorder sequence.
Sample Input
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1 代码示例 :
using namespace std;const int eps = 1e6+5;const int maxn = 0x3f3f3f3f;#define ll long longtypedef struct node{    int data;    struct node *lchild, *rchild;}bitnode, *bitree;int vis[1005];int a[1005], b[1005];int sign;int n;void build(bitree *T, int l, int r) {    int p;    for(int i = 0; i < n; i++) {        sign = 0;        if (!vis[a[i]]) {            for(int j = l; j <= r; j++){                if (a[i] == b[j]) {                    p = j;                    sign = 1;                    vis[a[i]] = 1;                     break;                }            }        }        if (sign) break;    }    if (!sign) return;    //printf(" p = %d \n", b[p]);    (*T) = (bitree)malloc(sizeof(bitnode));    (*T)->data = b[p];    (*T)->lchild = (*T)->rchild = NULL;    build(&(*T)->lchild, l, p-1);    build(&(*T)->rchild, p+1, r);}void order(bitree T, bitree p){    if (T) {         order(T->lchild, p);         order(T->rchild, p);        printf("%d%c", T->data, T==p?'\n':' ');      }}int main() {    bitree T;        while(~scanf("%d", &n)) {        memset(vis, 0, sizeof(vis));        for(int i = 0; i < n; i++) scanf("%d", &a[i]);        for(int i = 0; i < n; i++) scanf("%d", &b[i]);        build(&T, 0, n-1);        order(T, T);     }    return 0;}

 

 

转载于:https://www.cnblogs.com/ccut-ry/p/8301448.html

你可能感兴趣的文章
python 3.6 setup
查看>>
VB6进行GZIP解压&C#进行GZIP压缩和解压
查看>>
第9章 泛型
查看>>
05 吸收应用-会整理还不够?教你吸收、联想、输出、应用
查看>>
Selenium Web 自动化 - Selenium常用API
查看>>
51Nod 1058: N的阶乘的长度(斯特林公式)
查看>>
变量类型以及传递
查看>>
chrome-解决该扩展程序未列在 Chrome 网上应用店中
查看>>
ASP.NET Session原理及处理方法
查看>>
oracle中order by造成分页错误
查看>>
foxmail配置邮箱pop/IMAP
查看>>
Android 自定义坐标轴控件
查看>>
hdu1114 dp(完全背包)
查看>>
html5 canvas 移动小方块
查看>>
荷兰TAC的需求
查看>>
CSS3阴影 box-shadow的使用和技巧总结
查看>>
CodeChef Sereja and LCM(矩阵快速幂)
查看>>
实习日志(1)2011-12-30
查看>>
XML序列化与反序列化(续)
查看>>
实现简单的时间显示
查看>>