[二叉树建树] 后序遍历与中序遍历建立二叉树
1020. Tree Traversals (25)
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
[二叉树建树] 后序遍历与中序遍历建立二叉树相关推荐
- java根据前序和中序建树_Java实现根据前序遍历构建二叉树(前序遍历、中序遍历、后序遍历)...
Java实现根据前序遍历构建二叉树(前序遍历.中序遍历.后序遍历),Java关于ACM的代码真的好少,想参考如何用java实现二叉树googl 前言 Java关于ACM的代码真的好少,想参考如何用ja ...
- 二叉树:通过前序遍历与中序遍历序列输出二叉树的后序遍历序列
题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树: 中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树: 后序遍历: ...
- 【数据结构笔记10】二叉树的先序、中序、后序遍历,中序遍历的堆栈/非递归遍历算法,层序遍历,确定一个二叉树,树的同构
本次笔记内容: 3.3.1 先序中序后序遍历 3.3.2 中序非递归遍历 3.3.3 层序遍历 3.3.4 遍历应用例子 小白专场:题意理解及二叉树表示 小白专场:程序框架.建树及同构判别 文章目录 ...
- 【二叉树Java】二叉树遍历前序中序后序遍历的非递归写法
本文主要介绍二叉树前序中序后序遍历的非递归写法 在探讨如何写出二叉树的前序中序后序遍历代码之前,我们先来明确一个问题,前序中序后序遍历根据什么区分? 二叉树的前序中序后序遍历,是相较根节点说的.最先遍 ...
- python实现二叉树遍历(前序遍历、中序遍历、后序遍历)
python实现二叉树遍历(前序遍历.中序遍历.后序遍历) 在计算机科学中,二叉树是一种树数据结构,其中每个节点最多有两个子节点,称为左子节点和右子节点.使用集合理论概念的递归定义是(非空)二叉树是元 ...
- 手动创建一棵二叉树,然后利用前序、中序、后序、层序进行遍历(从创建二叉树到各种方式遍历)(含运行结果)
手动创建一棵二叉树,然后利用前序.中序.后序.层序进行遍历 import java.util.LinkedList; import java.util.List; import java.util.Q ...
- 二叉树的前序遍历,中序遍历,后序遍历学习 (原)
经验: 不要死记各个遍历节点的位置,将一个复杂的二叉树当作一个个小的二叉树学习前序遍历,中序遍历,后序遍历会更容易理解 转载于:https://www.cnblogs.com/gyrgyr/p/962 ...
- 已知二叉树的前序遍历、中序遍历或者中序遍历、后序遍历求二叉树结构的算法
二叉树中的前序遍历是先访问根结点,再访问左子树,右子树. 中序遍历是先访问左子树,再是根结点,最后是右子树. 后序遍历是先访问左子树,再是右子树,最后是根结点. 算法思路是先根据前序遍历的第一个结点或 ...
- 二叉树 —— 创建二叉树 先序遍历 、中序遍历、后序遍历(递归方式、非递归方式)
#include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef char DataType; #de ...
- 【❤️算法系列之顺序二叉树的实现(前序遍历、中序遍历、后序遍历)❤️】
1.何为顺序二叉树 2.顺序二叉树的特点 3.顺序二叉树的遍历 3.1.前序遍历 3.2.中序遍历 3.3.后序遍历 4.顺序二叉树的注意点 [❤️算法系列之二叉树的实现(包含前序.中序.后序遍历以及 ...
最新文章
- python 实现延迟的操作
- MinGw 和 cygwin 的区别和联系 (跨平台 windows运行linux程序)
- 【内网穿透】生壳SSH映射 for Linux 使用教程
- 使用URI设计松散耦合的Metro应用程序
- 广科计算机专业3 2分数线,考试网:重庆最新高考录取分数线发布 理科一本533分...
- [转载] python 短网址_使用Python生成url短链接的方法
- 康博(COMPUWARE)软件公司简介
- 385.迷你语法分析器
- C/C++ _beginthreadex 多线程操作 - 线程同步
- wps2019将表格数据转换成工资条的操作方法
- 文件服务器资源管理器类似软件,好用的小众文件管理软件推荐给大家
- 芯邦主控的U盘量产教程
- Codewar刷题总结
- Vivado工程配置petalinux实现linux下网卡驱动
- 全国(大学)高等教育各学科视频教学全集
- 如何给MySQL 数据瘦身
- Jetpack Compose——Icon(图标)的使用
- vue所有页面刷新一次mounted(以及所有生命周期函数)执行两次的解决方法
- JQuery入门(1) - 选择器
- 计算机右键无法新建excel2007,电脑桌面右键新建菜单中没有Word/Excel/PPT等文档怎么办?...