题目内容:

我们知道如何按照三种深度优先次序来周游一棵二叉树,来得到中根序列、前根序列和后根序列。反过来,如果给定二叉树的中根序列和后根序列,或者给定中根序列和前根序列,可以重建一二叉树。本题输入一棵二叉树的中根序列和后根序列,要求在内存中重建二叉树,最后输出这棵二叉树的前根序列。

用不同的整数来唯一标识二叉树的每一个结点,下面的二叉树

中根序列是9 5 32 67

后根序列9 32 67 5

前根序列5 9 67 32

输入格式:

两行。第一行是二叉树的中根序列,第二行是后根序列。每个数字表示的结点之间用空格隔开。结点数字范围0~65535。暂不必考虑不合理的输入数据。

输出格式:

一行。由输入中的中根序列和后根序列重建的二叉树的前根序列。每个数字表示的结点之间用空格隔开。

【题解】

后根序列的最后一个元素即为二叉树的树根root。root将中根序列分为两部分,左半边是左子树的中根序列,而右半边则是右部分的中根序列。同时后根序列依照左子树和右子树节点数也可以被分为左子树的后根序列和右子树的后根序列。于是便可依此递归地按左右子树的后根、中根序列重建子树,最终重建二叉树。

【代码】

 #include <iostream>
using namespace std;
/*build BT from its inorder and postorder*/ constexpr int MAXN = ;
int postorder[MAXN], inorder[MAXN]; struct btn
{
int data;
btn *left;
btn *right;
}; btn *build_tree(int io1, int io2, int po1, int po2)
{
/*io1, io2 are the begining and ending points of inorder sequence respectively*/
/*po1, po2 are the begining and ending points of postorder sequence respectively*/
int i = , ion = io2 - io1 + ;
btn* root = new btn;
root->data = postorder[po2];
root->left = NULL;
root->right = NULL;
for (i = ; i < ion; i++) {
if (root->data == inorder[io1 + i])break;
}
if (i > ) root->left = build_tree(io1, io1 + i - , po1, po1 + i - );
if (io1 + i < io2) root->right = build_tree(io1 + i + , io2, po1 + i, po2 - );
return root;
} void preorder(btn *root, int vnum, int &count)
{
if (root != NULL) {
cout << root->data;
if (count < vnum - ) {
cout << " ";
count++;
preorder(root->left, vnum, count);
preorder(root->right, vnum, count);
}
else cout << endl;
}
} void deletetree(btn *root)
{
if (root != NULL) {
delete(root->left);
delete(root->right);
delete root;
root = NULL;
}
} int main()
{
int c = , i = , vnum = ;
while (cin >> inorder[i++]) {
if (cin.get() != ' ')break;
/*Although cin will skip the blank space, the cursor stays at the blank after cin reads a number,thus cin.get() can fetch the
blank space.*/
} vnum = i;
i = ;
while (cin >> postorder[i++]) {
if (cin.get() != ' ')break;
}
btn *root = build_tree(, i - , , i - );
preorder(root, vnum, c);
deletetree(root);
return ;
}

OpenJudge 由中根顺序和后根序列重建二叉树的更多相关文章

  1. 剑指Offer 通过中序和先序遍历重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  2. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

  3. 用前序和中序重建二叉树 python

    程序实现了用二叉树的前序遍历序列和中序遍历序列重建二叉树,代码用python实现. 首先定义二叉树节点的类: class TreeNode: def __init__(self, x): self.v ...

  4. LeetCode竞赛题:笨阶乘(我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。)

    通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.相反,我们设计了一个笨 ...

  5. C++实现二叉树的链接存储结构(先根、中根和后根遍历)

    验证二叉树的链接存储结构及其上的基本操作. [实验要求]: 1. 从文件创建一棵二叉树,并对其初始化: 2. 先根.中根.后根遍历二叉树: 3. 在二叉树中搜索给定结点的父结点: 4. 搜索二叉树中符 ...

  6. 【LeetCode】105#从前序与中序遍历序列构造二叉树

    题目描述 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9 ...

  7. LeetCode105 从前序和中序序列构造二叉树

    题目描述: 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9 ...

  8. 已知前序(后序)遍历序列和中序遍历序列构建二叉树(Leetcode相关题目)

    1.文字描述: 已知一颗二叉树的前序(后序)遍历序列和中序遍历序列,如何构建这棵二叉树? 以前序为例子: 前序遍历序列:ABCDEF 中序遍历序列:CBDAEF 前序遍历先访问根节点,因此前序遍历序列 ...

  9. Tree Recovery(由先、中序列构建二叉树)

    题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary tr ...

随机推荐

  1. How to scale Complex Event Processing (CEP)/ Streaming SQL Systems?

    转自:https://iwringer.wordpress.com/2012/05/18/how-to-scale-complex-event-processing-cep-systems/ What ...

  2. Gravitational Teleport简单使用

    使用官方提供的二进制包进行快速启动测试,详细细节还需要在学习 下载软件包 mac 系统 https://gravitational.com/teleport/download/ wget https: ...

  3. openresty 编译ngx_pagespeed 模块-docker 构建

    ngx_pagespeed 是一个很不错的web 优化模块,我们通过简单的配置就可以对于web页面的加载有很大的提升 ngx_pagespeed 依赖psol 模块 Dockerfile   # Do ...

  4. lapis 项目添加prometheus 监控

      lapis 是基于openresty 扩展的,所以直接将支持prometheus的模块构建进openresty 就可以了 我使用的是nginx-module-vts 模块 环境准备 我已经构建好了 ...

  5. IAR intrinsic functions

    You can insert asm code example asm("NOP") into the c or c++ source code to get a good per ...

  6. nginx+php windows安装配置

    https://blog.csdn.net/zjiang1994/article/details/72876193 https://blog.csdn.net/bruce_wang_janet/art ...

  7. 笔记:Python 默认参数必须指向不变对象

    笔记:Python 默认参数必须指向不变对象 学习记录 >>> def add_end(L=[]): L.append('END') return L >>> ad ...

  8. openstack--2--控制节点安装mysql和rabbitmq

    生产中可以把mysql数据库单独安装到一台机器上,这里因为实验机器有限,就把mysql安装到了控制节点 其实openstack每个组件都可以安装到单独的机器上. RabbitMQ介绍 RabbitMQ ...

  9. 单节点 Elasticsearch 出现 unassigned shards 原因及解决办法

    根本原因: 是因为集群存在没有启用的副本分片,我们先来看一下官网给出的副本分片的介绍: 副本分片的主要目的就是为了故障转移,正如在 集群内的原理 中讨论的:如果持有主分片的节点挂掉了,一个副本分片就会 ...

  10. Logstash的grok以及Ruby

    logstash的grok插件的用途是提取字段,将非格式的内容进行格式化, input { file { path => "/var/log/http.log" } } fi ...