题目大意:

给定一棵树的结点个数n,以及它的前序遍历和后序遍历,输出它的中序遍历;

如果中序遍历不唯一就输出No,且输出其中一个中序即可,如果中序遍历唯一就输出Yes,并输出它的中序

思路:(转载

先序+后序 无法判断二叉树的唯一性

  • 先序第一个节点是根节点,紧接着第二个节点是根节点的左节点还是右节点?
  • 在后序中查找 先序中的第二个节点,
  1. 如果后序中的该位置 到 最后(也就是后序中根节点位置) 还有其他数的话,可以判断,先序中第二个节点肯定左节点(反证法。。。)
  2. 当中间没有数的时候,就不确定了

例如:

前序序列:
后序序列: 为 根, 接着,2在后序中,与1隔着两个数,所以2一定是1的左节点; ,4成为1的右子树节点
#include<bits/stdc++.h>
using namespace std;
int n;
bool isUnique = true;
vector<int> preOrder, in, postOrder; struct Node {
int val;
Node* lchild, *rchild;
Node(int _val = -){
val = _val;
lchild = NULL;
rchild = NULL;
} }; Node* Create(int preL, int preR, int postL, int postR)
{
Node* root = new Node(preOrder[preL]);
if (preL == preR) return root;
int k;
for (k = postL; k < postR; k++){ // 后序找 pre[preL + 1]
if (postOrder[k] == preOrder[preL + ]) break;
}
// 在后序中查找 先序中的第二个节点
// 如果后序中的该位置 到 最后(也就是后序中根节点位置) 还有其他数的话,
// 可以判断,先序中第二个节点肯定左节点(反证法。。。)
if (postR - k - > ){
int numLeft = k - postL + ;
root->lchild = Create(preL + , preL + numLeft, postL, k);
root->rchild = Create(preL + + numLeft, preR, k + , postR - );
}
else {
isUnique = false;
//假定第二个节点是根节点的右节点
root->rchild = Create(preL + , preR, postL, postR - );
}
return root;
} void inOrder(Node* root){
if (root != NULL){
inOrder(root->lchild);
in.push_back(root->val);
inOrder(root->rchild);
}
} int main()
{
scanf("%d", &n);
preOrder.resize(n);
postOrder.resize(n);
for (int i = ; i < n; i++) scanf("%d", &preOrder[i]);
for (int i = ; i < n; i++) scanf("%d", &postOrder[i]);
Node* root = Create(, n - , , n - );
inOrder(root);
printf("%s\n%d", isUnique == true ? "Yes" : "No", in[]);
for (int i = ; i<n; i++) printf(" %d", in[i]);
printf("\n");
return ;
}

1119.(重、错)Pre- and Post-order Traversals的更多相关文章

  1. Construct a tree from Inorder and Level order traversals

    Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...

  2. 解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat

    解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat ...

  3. git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题

    在同步本地文件到线上仓库的时候 报错 pre -commit hook failed (add --no-verify to bypass) 当你在终端输入git commit -m "xx ...

  4. 错题重错之枪战Maf

    题目描述 有 n 个人,用1∼n 进行编号,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的人 ...

  5. MySQL ORDER BY主键id加LIMIT限制走错索引

    背景及现象 report_product_sales_data表数据量2800万: 经测试,在当前数据量情况下,order by主键id,limit最大到49的时候可以用到索引report_produ ...

  6. Win7系统64位环境下使用Apache——安装Apache2.4时报错“Invalid command Order”问题的解决

    之前在文章Win7系统64位环境下使用Apache--Apache2.4整合Tomcat与mod_jk提到了安装Apache2.4时有可能报错: Invalid command 'Order', pe ...

  7. SQL注入之Sqli-labs系列第五关和第六关(基于GET型的报错注入)

    废话不在多说  let's go! 开始挑战第五关(Double Query- Single Quotes- String) 和第六关(Double Query- Double Quotes- Str ...

  8. SQL Server中ORDER BY后面可以是表达式和子查询

    假如SQL Server数据库中现在有Book表如下 CREATE TABLE [dbo].[Book]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [CreateTi ...

  9. SQLi-LABS Page-3 (order by injections) Less-46-Less-53

    关于order by 注入原理讲解 注入原理 1. 测试 ?sort=1 desc--+?sort=1 asc--+ 显示结果不同,说明可以注入 desc是 descend 降序意思 asc 是 as ...

  10. HDU 1160 FatMouse's Speed

    半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...

随机推荐

  1. NCPC 2016:简单题解

    A .Artwork pro:给定N*M的白色格子,然后Q次黑棒,输出每次加黑棒后白色连通块的数量.(N,M<1e3, Q<1e4) sol:倒着离线做,并查集即可. (在线做法:http ...

  2. 论container的前世今生

    why Normally, thin-client multitiered applications are hard to write because they involve many lines ...

  3. angular的点击添加

    首先是在js里面我们可以用clone来点击添加一些东西比如列表或者其他的div之类的,但是在angular里面怎么实现点击添加呢? 类似这种: 这样就尴尬了,最少我这样的菜鸟是不知道怎么去写的,网上好 ...

  4. sscanf 与 ssprintf 用法 (转载--https://www.cnblogs.com/Anker/p/3351168.html)

    sprintf函数 sprintf函数原型为 int sprintf(char *str, const char *format, ...).作用是格式化字符串,具体功能如下所示: (1)将数字变量转 ...

  5. cvtColor()学习

    CvtColor Void cv::cvtColor(InputArray src, OutputArray dst, INT code, INT dstCn = ) 将图像从一个颜色空间转换为另一个 ...

  6. acm 2005

    ////////////////////////////////////////////////////////////////////////////////#include<iostream ...

  7. [动态差分+二维前缀和][小a的轰炸游戏]

    链接:https://ac.nowcoder.com/acm/contest/317/E来源:牛客网 题目描述 小a正在玩一款即时战略游戏,现在他要用航空母舰对敌方阵地进行轰炸 地方阵地可以看做是n× ...

  8. linux http配置

    yum install httpd 安装http服务器 启动http服务器即可访问 如果不行的话,试着执行命令 firewall-cmd –permanent –add-service=http(该命 ...

  9. 网络-console

    console接口h3c er8300cisco asaQuidway S5700-28C-SI Routing Switchtopsec <H3C>? reboot Reboot dev ...

  10. cos migration工具webhook推送

    上一篇讲了腾讯云同步工具的使用,这篇主要是补充如何将同步结果主动消息通知. 因为cos migration 工具是java语言,并在github开源的,所以可以直接修改源码,添加webhook推送代码 ...