博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aizu - ALDS1_7_d Reconstruction of a Tree 树的重建
阅读量:3905 次
发布时间:2019-05-23

本文共 1347 字,大约阅读时间需要 4 分钟。

Write a program which reads two sequences of nodes obtained by the preorder tree walk and the inorder tree walk on a binary tree respectively, and prints a sequence of the nodes obtained by the postorder tree walk on the binary tree.

Input

In the first line, an integer nn, which is the number of nodes in the binary tree, is given.

In the second line, the sequence of node IDs obtained by the preorder tree walk is given separated by space characters.
In the second line, the sequence of node IDs obtained by the inorder tree walk is given separated by space characters.

Every node has a unique ID from 11 to nn. Note that the root does not always correspond to 11.

Output

Print the sequence of node IDs obtained by the postorder tree walk in a line. Put a single space character between adjacent IDs.

Constraints

  • 1≤n≤401≤n≤40

Sample Input 1

51 2 3 4 53 2 4 1 5

Sample Output 1

3 4 2 5 1

Sample Input 2

41 2 3 41 2 3 4

Sample Output 2

4 3 2 1

 给出先序遍历和中序遍历,求后序遍历。

可以通过遍历先序,确定节点的左右子树,然后求解。

代码如下:

 

#include 
#include
#include
#include
#include
using namespace std;int n;int loc=0;vector
pre,in,post;int finds (int x){ for (int i=0;i
=r) return; int root=pre[loc++]; int m=finds(root); rec(l,m); rec(m+1,r); post.push_back(root);}void output (){ rec (0,n); for (int i=0;i

 

转载地址:http://wvaen.baihongyu.com/

你可能感兴趣的文章
linux sysctl 参数实现 暨 ip_forward参数对Linux内核转发影响分析
查看>>
linux 路由表 的一些相关资料
查看>>
Linux 路由 学习笔记 之三 路由查找流程分析
查看>>
LINUX IP 路由实现
查看>>
快速重传与快速恢复算法
查看>>
TCP重传定时器
查看>>
CentOS 6.3 - 安装 Nginx 1.2.7(yum源)
查看>>
shell中trap捕获信号
查看>>
关于Linux Shell的信号trap功能你必须知道的细节
查看>>
Linux原始套接字实现分析
查看>>
CENTOS 6.5 配置YUM安装NGINX
查看>>
#ifdef DEBUG的理解
查看>>
Linux 任务控制的几个技巧( &, [ctrl]-z, jobs, fg, bg, kill)
查看>>
慧眼云:基于云计算和大数据分析的主动防御实践
查看>>
58集团监控业务实践:将网站运行信息透明化
查看>>
给Django用户的SQLAlchemy介绍
查看>>
consul http api
查看>>
如何定位问题
查看>>
使用火焰图分析CPU性能回退问题
查看>>
openresty lua zlib整合安装 让lua支持解压服务端压缩过的数据
查看>>