HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2442 Accepted Submission(s): 1063
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.
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
题意:
给你一个前序遍历和中序遍历,要求后序。
可以由先序和中序的性质得到 : 先序的第一个借点肯定是当前子树的根借点, 那么在
中序中找到这个结点, 则这个结点左边的节点属于左子树, 右边的属于右子树。然后递归遍历就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack> using namespace std; const int N=; int n,pre[N],in[N]; //先序数组和后序数组
stack<int> st; //存放父节点 void build(int l1,int r1,int l2,int r2){ //l1,r1,是先序遍历的数组的开始和末尾,l2,r2是中序遍历的数组的开始和末尾
int i,j;
st.push(pre[l1]); //父节点入栈
for(i=l2;i<=r2;i++)
if(in[i]==pre[l1]) //找到父节点在中序遍历的位置i
break;
j=l1+(i-l2+); //确定左子树和右子树在先序遍历的分界点j,即右子树的父节点
if(j<=r1 && i+<=r2) //求解右子树
build(j,r1,i+,r2);
if(l1+<=j- && l2<=i-) //求解左子树
build(l1+,j-,l2,i-);
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){
for(int i=;i<n;i++)
scanf("%d",&pre[i]);
for(int i=;i<n;i++)
scanf("%d",&in[i]);
build(,n-,,n-);
while(!st.empty()){
printf("%d",st.top());
st.pop();
if(!st.empty())
printf(" ");
}
printf("\n");
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<cstdlib> using namespace std; const int N=; struct Tree{
Tree *l,*r;
int x;
}tree; Tree *root; Tree *Create(int *pre,int *in,int n){
Tree *tmp;
for(int i=;i<n;i++)
if(pre[]==in[i]){ //找到中序遍历时的根节点
tmp=(Tree *)malloc(sizeof(Tree));
tmp->x=in[i];
tmp->l=Create(pre+,in,i); //中序历遍中在根节点左边的都是左子树上的
tmp->r=Create(pre+i+,in+i+,n-(i+)); //在根节点右边的,都是右子树上的,右子树需要从i+1开
return tmp;
}
return NULL;
} void PostOrder(Tree *rt){ //后序历遍
if(rt!=NULL){
PostOrder(rt->l);
PostOrder(rt->r);
if(rt==root)
printf("%d\n",rt->x);
else
printf("%d ",rt->x);
}
} int main(){ //freopen("input.txt","r",stdin); int n,pre[N],in[N]; //先序数组和后序数组
while(~scanf("%d",&n)){
root=NULL;
for(int i=;i<n;i++)
scanf("%d",&pre[i]);
for(int i=;i<n;i++)
scanf("%d",&in[i]);
root=Create(pre,in,n);
Tree *rt=root;
PostOrder(rt);
}
return ;
}
HDU 1710 Binary Tree Traversals (二叉树遍历)的更多相关文章
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
- HDU 1710 Binary Tree Traversals(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- 【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...
- HDU 1710 Binary Tree Traversals
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- hdu1710 Binary Tree Traversals(二叉树的遍历)
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjo ...
随机推荐
- object-c 混编 调用C,C++接口
xcode 支持 object-c 混编,在object-c 中调用c,c++接口 第一步 定义c语言 接口(File.c) #include <stdio.h> void printsB ...
- (算法)从0到n整数中数字2出现的次数
题目: 数出0到n(含)中数字2出现了几次. 思路: 1.暴力方法,数出每个数字包含几个2,然后累加起来. 2.分析:分别考虑数字n每一位出现2的次数,如123123: 从左往右考虑4123123: ...
- spring boot使用slf4j输出日志
spring boot使用slf4j输出日志 https://blog.csdn.net/qq442270636/article/details/79406346 Spring Boot SLF4J日 ...
- windows安装mycat(转)
http://blog.csdn.net/sc9018181134/article/details/53063798 1.先到github上下载mycat 2.下载完成后,解压.应该是这样一个样子 3 ...
- Maven的JAR包仓库,不用再百度搜JAR包了!
http://search.maven.org/ 今天初学Maven,发现Maven的中央仓库里差点儿什么jar都有...........还有各种版本号... 你值得拥有!
- python反编译chm文件并生成pdf文件
# -*- coding: utf-8 -*- import os import os.path import logging import pdfkit original_chm = r'C:\Us ...
- Git如何获得两个版本间所有变更的文件列表
https://segmentfault.com/q/1010000000133613 git diff --name-status HEAD~2 HEAD~3
- Android内存分析命令(转)
一.概述 1.1 内存指标概念 Item 全称 含义 等价 USS Unique Set Size 物理内存 进程独占的内存 PSS Proportional Set Size 物理内存 PSS= U ...
- java HMAC_SHA1加密算法
java HMAC_SHA1加密算法 CreationTime--2018年7月14日16点46分 Author:Marydon 1.准备工作 import javax.crypto.Mac; i ...
- kettle Spoon.bat运行闪退
1.情景展示 启动kettle的Spoon.bat闪退,并没有进入kettle的启动界面. 2.原因分析 使用条件: jdk版本需>=1.6: java需配置环境变量. 如果满足了上述前提条 ...