[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)
1119. Pre- and Post-order Traversals (30)
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.
Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.
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 preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first printf in a line "Yes" if the tree is unique, or "No" if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. 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 1:
7
1 2 3 4 6 7 5
2 6 7 4 5 3 1
Sample Output 1:
Yes
2 1 6 4 7 3 5
Sample Input 2:
4
1 2 3 4
2 4 3 1
Sample Output 2:
No
2 1 3 4
分析:目前,关于二叉树的遍历算法,有前序和中序,后序和中序,层序和中序,这三种组合都是可以唯一确定一颗二叉树的,目前都有用代码实现。其中,前序和中序建树和后序和中序建树两种方法都是想办法分出左右子树,然后对左右字数进行递归建树。层序和中序也可以用递归实现,但是目前我只用了非递归的实现方法。关于今天的建树方法,使用的是二叉树的前序遍历和后序遍历。但是我们知道,仅有前序遍历和后序遍历是没法确定一颗二叉树的。这里,前序遍历和后序遍历在某些调剂下可以唯一确定一颗二叉树。但这里不做这个要求。题目的要求是当产生歧义是可随意建立一颗满足前序和后序遍历的二叉树即可。
目前已经碰到了二叉树建树的大多数情况,下次有时间稍微总结一下。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; vector<int > pre,post,in; struct Node
{
int data;
Node *l,*r;
}; bool flag=true; void creat(Node * & root,int preL,int preR,int postL,int postR)
{
if(preL==preR)
{
root=new Node;
root->data=pre[preL];
root->l=root->r=NULL;
return ;
}
root=new Node;
root->data=pre[preL];
root->l=root->r=NULL;
int i,j;
for(i=postL;i<=postR;i++)
{
if(post[i]==pre[preL+])
{
break;
}
}
int leftNum=i-postL;
if(post[i]==post[postR-])//不确定的条件,无法区分是左子树还是右子树
{
flag=;
creat(root->l,preL+,preR,postL,postR-);
}
else
{
creat(root->l,preL+,preL++leftNum,postL,postL+leftNum);
creat(root->r,preL+leftNum+,preR,postL+leftNum+,postR-);
}
} vector<int> ans; void inOrder(Node * root)
{
if(root!=NULL)
{
inOrder(root->l);
ans.push_back(root->data);
inOrder(root->r);
}
} void outans()
{
for(int i=;i<ans.size();i++)
{
if(i>) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int k;
scanf("%d",&k);
pre.push_back(k);
}
for(int i=;i<n;i++)
{
int k;
scanf("%d",&k);
post.push_back(k);
}
Node * root=NULL;
creat(root,,n-,,n-);
inOrder(root);
if(flag==true) printf("Yes\n");
else printf("No\n");
outans();
return ;
}
[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)的更多相关文章
- PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一
Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- golang 实现海明距离 demo
Simhash的算法简单的来说就是,从海量文本中快速搜索和已知simhash相差小于k位的simhash集合,这里每个文本都可以用一个simhash值来代表,一个simhash有64bit,相似的文本 ...
- SSM-CRUD入门项目——环境搭建
一.项目概述 项目功能点: 1.分页 2.数据校验: jQuery前端校验+JSR303后端校验 3.ajax 4.RESTful风格的URI 技术点: 1.基础框架——SSM 2.数据库——MySQ ...
- 2017-2018-2 20155315《网络对抗技术》Exp1:PC平台逆向破解
实验目的 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- 学习笔记:启动对特定用户的会话的sql跟踪
学习 Oracle性能诊断艺术 制作一个role: CREATE ROLE sql_trace; 然后再作一个数据库登录级别的触发器: CREATE OR REPLACE TRIGGER enable ...
- 1. [文件]- 文件类型,文件open模式
1.文件类型:文本文件和二进制文件 硬盘中的文件保存为01010101格式,一般读取文件是把文件从硬盘中读取到内存中. 文本文件需要进行格式转换才能读取出来. 二进制文件一般用于传输 二进制文件:视频 ...
- P5231 [JSOI2012]玄武密码
P5231 [JSOI2012]玄武密码 链接 分析: 首先对所有询问串建立AC自动机,然后扫描一遍母串,在AC自动机上走,没走到一个点,标记这个点走过了,并且它的fail树上的祖先节点也可以访问到( ...
- linux下centos7中mysql崩溃问题的解决
---恢复内容开始--- 出现错误: 尝试解决: 错误解释是说系统运行过程中丢失了pid:我最先想到是 可能磁盘满了:于是 df -h 检查了一下:磁盘并没有满! 于是我对/etc/my.cnf [m ...
- Package设计1:选择数据类型、暂存数据和并发
SSIS 设计系列: Package设计1:选择数据类型.暂存数据和并发 Package设计2:增量更新 Package 设计3:数据源的提取和使用暂存 一,数据类型的选择 对于SSIS的数据类型,容 ...
- Apache和Nginx比较
Apache和Nginx对比 功能对比 Nginx和Apache一样,都是HTTP服务器软件,在功能实现上都采用模块化结构设计,都支持通用的语言接口,如PHP.Perl.Python等,同时还支持正向 ...
- 开源第三方登录组件OAuthLogin2.0 架构解析及开源地址
OAuthLogin2.0介绍地址: 入门地址:http://www.cnblogs.com/dazhuangtage/p/6306133.html Nuget地址:https://www.nuget ...