【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710
【题意】
给定一棵二叉树的前序遍历和中序遍历,输出后序遍历
【思路】
根据前序遍历和中序遍历递归建树,再后续遍历输出
malloc申请空间在堆,函数返回,内存不释放,需要free手动释放
【Accepted】
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm> using namespace std;
const int maxn=1e3+;
int n;
int pre[maxn],in[maxn],post[maxn];
int mp[maxn];
struct node{
int id;
node *lef;
node *rig;
node(int i, node *l=NULL, node *r=NULL):id(i),lef(l),rig(r){}
};
node* dfs(int l,int r,int x,int y){
if(l>r) return NULL;
node *nd=(node *)malloc(sizeof(node));
nd->id=in[y];
nd->lef=dfs(l,y-,x+,mp[pre[x+]]);
nd->rig=dfs(y+,r,x+y-l+,mp[pre[x+y-l+]]);
return nd;
}
void postorder(node *root){
if(root==NULL) return;
postorder(root->lef);
postorder(root->rig);
if(root->id==pre[]){
printf("%d\n",root->id);
}else{
printf("%d ",root->id);
}
free(root);
}
int main(){
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
scanf("%d",&pre[i]);
}
for(int i=;i<n;i++){
scanf("%d",&in[i]);
mp[in[i]]=i;
}
if(n==) {
printf("1\n");
continue;
}
node *root=dfs(,n-,,mp[pre[]]);
postorder(root);
}
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 (二叉树遍历)
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(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- 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
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- log explorer使用的几个问题[转载]
1)对数据库做了完全 差异 和日志备份备份时选用了删除事务日志中不活动的条目再用Log explorer打试图看日志时提示No log recorders found that match the f ...
- Codeforces Round #317 (Div. 2) C Lengthening Sticks (组合,数学)
一个合法的三角形的充要条件是a<b+c,其中a为最长的一边,可以考虑找出所有不满足的情况然后用总方案减去不合法的情况. 对于一个给定的总长度tl(一定要分完,因为是枚举tl,不分配的长度已经考虑 ...
- HDU 4348 I - To the moon 可持续化
队友套的可持续化线段树,徘徊在RE和MLE之间多发过的... 复用结点新的线段树平均要log2N个结点. 其实离线就好,按照时间顺序组织操作然后dfs. #include <iostream&g ...
- C#中窗体边框隐藏
设置窗体属性 FormBorderStyle 为 None
- python_111_异常处理
#1 name=['a','s'] try: print(name[3]) except: print('list index out of range')#list index out of ran ...
- archlinux alsa安装,音量设置和音量信息保存
1,使用前确认安装了alsa-utils sudo pacman -S alsa-utils2,运行alsamixer调试音量 alsamixer左右键选择调哪个,将Master和PCM按“m”解除静 ...
- UNIX 进程间通讯(IPC)概念(Posix,System V IPC)
IPC(Inter-Process Communication,进程间通讯)可以有三种信息共享方式(随文件系统,随内核,随共享内存).(当然这里虽然说是进程间通讯,其实也是可以和线程相通的). 相对 ...
- abaqus二次开发概述
说明 abaqus二次开发概述 导语 用户子程序特点 abaqus用户程序接口与调用方式 abaqus用户子程序分类 常用用户子程序介绍 Refence 说明 本系列文章本人基本没有原创贡献,都是在学 ...
- HDU-2544-最短路(floyd)
板子题,实验一下floyd. #include <cstdio> #include <algorithm> #include <cstring> using nam ...
- fshc之请求仲裁机制的代码分析
always@(posedge spi_clk or negedge spiclk_rst_n) begin if(~spiclk_rst_n) arbiter2cache_ack_r <='b ...