1020. Tree Traversals (25)

时间限制
400 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

分析:根据题目所给的后序遍历序列和中序遍历序列,构建二叉树,再输出该二叉树的前序遍历序列。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;const int maxn=40;
int post[maxn];
int in[maxn];struct node
{int data;node *lchild,*rchild;
};node * creat(int postL,int postR,int inL,int inR)
{if(postL>postR) return NULL;node * root=new node;root->data=post[postR];int index;for(index=inL;index<=inR;index++){if(in[index]==post[postR]){break;}}int numLeft=index-inL;root->lchild=creat(postL,postL+numLeft-1,inL,index-1);root->rchild=creat(postL+numLeft,postR-1,index+1,inR);return root;
}int first_flag=0;void layerOrder(node * root)
{queue<node *> ans;ans.push(root);while(!ans.empty()){node * tmp=ans.front();ans.pop();if(first_flag!=0){cout<<" ";}cout<<tmp->data;first_flag=1;if(tmp->lchild!=NULL) ans.push(tmp->lchild);if(tmp->rchild!=NULL) ans.push(tmp->rchild);}
}int main()
{int n;cin>>n;for(int i=0;i<n;i++){cin>>post[i];}for(int i=0;i<n;i++){cin>>in[i];}node * root;root=creat(0,n-1,0,n-1);layerOrder(root);return 0;
}

转载于:https://www.cnblogs.com/xiongmao-cpp/p/6430988.html

[二叉树建树] 后序遍历与中序遍历建立二叉树相关推荐

  1. java根据前序和中序建树_Java实现根据前序遍历构建二叉树(前序遍历、中序遍历、后序遍历)...

    Java实现根据前序遍历构建二叉树(前序遍历.中序遍历.后序遍历),Java关于ACM的代码真的好少,想参考如何用java实现二叉树googl 前言 Java关于ACM的代码真的好少,想参考如何用ja ...

  2. 二叉树:通过前序遍历与中序遍历序列输出二叉树的后序遍历序列

    题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树: 中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树: 后序遍历: ...

  3. 【数据结构笔记10】二叉树的先序、中序、后序遍历,中序遍历的堆栈/非递归遍历算法,层序遍历,确定一个二叉树,树的同构

    本次笔记内容: 3.3.1 先序中序后序遍历 3.3.2 中序非递归遍历 3.3.3 层序遍历 3.3.4 遍历应用例子 小白专场:题意理解及二叉树表示 小白专场:程序框架.建树及同构判别 文章目录 ...

  4. 【二叉树Java】二叉树遍历前序中序后序遍历的非递归写法

    本文主要介绍二叉树前序中序后序遍历的非递归写法 在探讨如何写出二叉树的前序中序后序遍历代码之前,我们先来明确一个问题,前序中序后序遍历根据什么区分? 二叉树的前序中序后序遍历,是相较根节点说的.最先遍 ...

  5. python实现二叉树遍历(前序遍历、中序遍历、后序遍历)

    python实现二叉树遍历(前序遍历.中序遍历.后序遍历) 在计算机科学中,二叉树是一种树数据结构,其中每个节点最多有两个子节点,称为左子节点和右子节点.使用集合理论概念的递归定义是(非空)二叉树是元 ...

  6. 手动创建一棵二叉树,然后利用前序、中序、后序、层序进行遍历(从创建二叉树到各种方式遍历)(含运行结果)

    手动创建一棵二叉树,然后利用前序.中序.后序.层序进行遍历 import java.util.LinkedList; import java.util.List; import java.util.Q ...

  7. 二叉树的前序遍历,中序遍历,后序遍历学习 (原)

    经验: 不要死记各个遍历节点的位置,将一个复杂的二叉树当作一个个小的二叉树学习前序遍历,中序遍历,后序遍历会更容易理解 转载于:https://www.cnblogs.com/gyrgyr/p/962 ...

  8. 已知二叉树的前序遍历、中序遍历或者中序遍历、后序遍历求二叉树结构的算法

    二叉树中的前序遍历是先访问根结点,再访问左子树,右子树. 中序遍历是先访问左子树,再是根结点,最后是右子树. 后序遍历是先访问左子树,再是右子树,最后是根结点. 算法思路是先根据前序遍历的第一个结点或 ...

  9. 二叉树 —— 创建二叉树 先序遍历 、中序遍历、后序遍历(递归方式、非递归方式)

    #include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef char DataType; #de ...

  10. 【❤️算法系列之顺序二叉树的实现(前序遍历、中序遍历、后序遍历)❤️】

    1.何为顺序二叉树 2.顺序二叉树的特点 3.顺序二叉树的遍历 3.1.前序遍历 3.2.中序遍历 3.3.后序遍历 4.顺序二叉树的注意点 [❤️算法系列之二叉树的实现(包含前序.中序.后序遍历以及 ...

最新文章

  1. python 实现延迟的操作
  2. MinGw 和 cygwin 的区别和联系 (跨平台 windows运行linux程序)
  3. 【内网穿透】生壳SSH映射 for Linux 使用教程
  4. 使用URI设计松散耦合的Metro应用程序
  5. 广科计算机专业3 2分数线,考试网:重庆最新高考录取分数线发布 理科一本533分...
  6. [转载] python 短网址_使用Python生成url短链接的方法
  7. 康博(COMPUWARE)软件公司简介
  8. 385.迷你语法分析器
  9. C/C++ _beginthreadex 多线程操作 - 线程同步
  10. wps2019将表格数据转换成工资条的操作方法
  11. 文件服务器资源管理器类似软件,好用的小众文件管理软件推荐给大家
  12. 芯邦主控的U盘量产教程
  13. Codewar刷题总结
  14. Vivado工程配置petalinux实现linux下网卡驱动
  15. 全国(大学)高等教育各学科视频教学全集
  16. 如何给MySQL 数据瘦身
  17. Jetpack Compose——Icon(图标)的使用
  18. vue所有页面刷新一次mounted(以及所有生命周期函数)执行两次的解决方法
  19. JQuery入门(1) - 选择器
  20. 计算机右键无法新建excel2007,电脑桌面右键新建菜单中没有Word/Excel/PPT等文档怎么办?...

热门文章

  1. static、final、abstract基本作用
  2. linux 内存显示括号内字母的含义
  3. dokuwiki 的管理和使用(补充)
  4. Head First JSP---随笔五
  5. 【快乐水题】2000. 反转单词前缀
  6. Android AOP之字节码插桩
  7. 新一代开源Android渠道包生成工具Walle
  8. 开源资产管理系统_开源cmdb来啦 通用CMDB 开源资产管理系统
  9. JZOJ 5909. 【NOIP2018模拟10.16】跑商(paoshang)
  10. 2能不用cuda_cuda学习-1-cufft的使用