已知后序与中序输出前序(先序):
后序:3, 4, 2, 6, 5, 1(左右根)
中序:3, 2, 4, 1, 6, 5(左根右)

已知一棵二叉树,输出前,中,后时我们采用递归的方式。同样也应该利用递归的思想:

对于后序来说,最后一个节点肯定为根。在中序中可以找到左子树的个数,那么就可以在后序中找到左子树的根;同理也可以找到右子树。

void pre(int root, int start, int end) {
if(start > end) return ;
int i = start;
while(i < end && in[i] != post[root]) i++;
printf("%d ", post[root]);
pre(root - - end + i, start, i - );
pre(root - , i + , end);
}

如果想要按照层次输出数,那么我们可以增加一个index变量,来存储结点。根节点为0,左子树为index*2+1, 右子树为index*2+2,那么我们按顺序输出不是-1的节点即可

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
vector<int>lever(,-),post,in;
int n;
void pre(int root,int start,int end,int index)
{
if(start>end)
return;
int i=start;
while(i<end&&in[i]!=post[root])
i++;
lever[index]=post[root];
pre(root-end+i-,start,i-,index*+);
pre(root-,i+,end,index*+);
}
int main()
{
scanf("%d",&n);
post.resize(n+);
in.resize(n+);
for(int i=;i<=n;i++)
scanf("%d",&post[i]);
for(int i=;i<+n;i++)
scanf("%d",&in[i]);
pre(n,,n,);
int cnt=;
for(int i=;i<lever.size();i++)
{
if(lever[i]!=-)
{
cnt++;
if(cnt==n)
{
printf("%d\n",lever[i]);
break;
}
else
printf("%d ",lever[i]);
}
}
}

PAT1020 (已知中序,后序遍历转前序遍历)的更多相关文章

  1. 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现

    public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...

  2. 已知树的前序、中序,求后序的c++实现&已知树的后序、中序,求前序的c++实现

    #include"iostream" using namespace std; int pre[30]; int in[30]; int post[30]; int indexOf ...

  3. TZOJ 3209 后序遍历(已知中序前序求后序)

    描述 在数据结构中,遍历是二叉树最重要的操作之一.所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问. 这里给出三种遍历算法. 1.中序遍历的递归算法定义:  ...

  4. SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)

    求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description  已知一 ...

  5. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...

  6. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  7. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. c++中二叉树的先序中序后序遍历

    c++中二叉树的先(前)序.中序.后序遍历 讲解版 首先先看一个遍历的定义(源自度娘): 所谓遍历(Traversal),是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问.访问结点所做的 ...

  9. 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现

    文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...

随机推荐

  1. docker 报ls: cannot open directory software/: Permission denied

    问题原因及解决办法 原因是CentOS7中的安全模块selinux把权限禁掉了,至少有以下三种方式解决挂载的目录没有权限的问题: 1.在运行容器的时候,给容器加特权,及加上 --privileged= ...

  2. docker资料转载

    Docker之理解image,container和storage-driver centos7创建docker tomcat镜像 Linux Namespace和Cgroup

  3. react-native android 报错 error calling Appregistry.runApplication

    解决了权限问题以为就没问题了,但是进来就红屏了,报错信息如下: 解决了,懒得截图了 error calling Appregistry.runApplication 这个问题也找了很久,开始找到 ht ...

  4. webpack环境搭建

    环境搭建参考:Webpack+vue+element逐步搭建开发环境 webpack入门:http://www.jianshu.com/p/42e11515c10f ----------------- ...

  5. python 阿狸的进阶之路(5)

    一.模块 1.什么是模块: 包含了一组功能的python文件,文件名是xxx.py,模块名是module. 可以使用 import module,四个通用的类别: (1)用python编写的py文件 ...

  6. jsp不解析el表达式,不识别jstl标签,找不到http://java.sun.com/jsp/jstl/core

    问题描述: jsp页面中el表达式,例如:${pageContext.request.contextPath},原样呈现,未被解析. 解决方案: 为jsp页添加page指令如下: <%@ pag ...

  7. html表单中get与post之间的区别

    当用户在 HTML 表单 (HTML Form) 中输入信息并提交之后,有两种方法将信息从浏览器传送到 Web 服务器 (Web Server). 一种方法是通过 URL,另外一种是在 HTTP Re ...

  8. ejs 用到的语法

    1.ejs 服务端渲染模板 2.语法: 01. <%= 变量名 %> -原样输出,不解析标签 02. <% js代码 %> 03. <%- 变量名%> -解析标签 ...

  9. cx_Oracle.DatabaseError: DPI-1047

    ODPI-C Installation¶ Overview Oracle Client and Database Versions Linux Oracle Instant Client Zip Or ...

  10. 1.Zabbix配置[仅环境搭建]

    Zabbix配置分为两部分1.Zabbix服务端的配置 安装Zabbix源: [root@c71 ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabb ...