1138 Postorder Traversal
题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值。
思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图。本题并不是让我们输出后序序列,而只是输出后序序列的第一个值!考虑到后序遍历的顺序是:左子树->右子树->根结点,思考一下,如果根结点存在左子树,那么后序序列的第一个值肯定在左子树中,这个时候右子树就不是我们关心的了;如果没有左子树,那么后序序列的第一个值就在右子树中。因此,我们只需要在常规的“构建二叉树”的函数中递归找到这个值就可以了,而不需要真的去重建二叉树。这么做代码量就少了很多。
代码:
#include <cstdio>
;
int pre[maxn],in[maxn];
int n;
int func(int preL,int preR,int inL,int inR)
{
if(preL==preR) return pre[preL];//边界
int pos=inL;
while(in[pos]!=pre[preL]) pos++;
int leftCnt=pos-inL;//左子树的结点个数
,preL+leftCnt,inL,pos-);//如果左子树存在,往左子树递归
,preR,pos+,inR);//左子树不存在,往右子树递归
}
int main()
{
scanf("%d",&n);
;i<n;i++) scanf("%d",&pre[i]);
;i<n;i++) scanf("%d",&in[i]);
printf(,n-,,n-));
;
}
1138 Postorder Traversal的更多相关文章
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1138 Postorder Traversal
https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...
- 1138. Postorder Traversal (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- PAT 1138 Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [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 Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
随机推荐
- Jexus部署Asp.Net Core项目
在之前的我的博客项目中,我将.net Core发布到Cent OS 上,使用的Nginx代理以及Supervisor进程守护,看过我的博客的童鞋,也会发现,这种方式比较麻烦,光命令行就看的头大,总共部 ...
- idea解决properties乱码问题
问题:我的IDEA已经将文件的字符集设置成了UTF-8,但是中文在*.properties文件中还是会出现乱码,后来经同事指点修改了一项配置就ok了!话不多说,看下面的对比就清楚了. 设置前: 设置后 ...
- nginx 相关资料
1.https://juejin.im/post/5a2600bdf265da432b4aaaba (nginx从入门到实践) 2.https://blog.csdn.net/hzsunshine/a ...
- eclipse配置tomcat运行项目访问不加项目名
- ARM的异常处理方式
1.什么是异常? 正常工作之外的流程都叫异常 异常会打断正在执行的工作,并且一般我们希望异常处理完成后继续回来执行原来的工作 中断是异常的一种 2.异常向量表 所有的CPU都有异常向量表,这是CPU设 ...
- Codeforces Round #394 (Div. 2) A. Dasha and Stairs
A. Dasha and Stairs time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Agilent RF fundamentals (2)- fundamental units of RF
1Amplitude AM调制 2 Frequency FM调制 3Phase Phase 调制 复合调制 三者关系:
- 转一篇pgpool配置
转一篇pgpool配置 http://dz.sdut.edu.cn/blog/subaochen/2013/08/postgresql-9-1的failover配置及其管理/ 环境介绍 在两台虚拟机上 ...
- 程序try-catch的绝对健壮性之嵌套
写程序的过程中,我们对try-catch在熟悉不过了,捕获异常进行处理,以保证程序的健壮性. 今日突发一想,如果我们catch中的代码异常了怎么办?我们做以下一种假设 static void Main ...
- 使用js构造"ddMMMyy"格式的日期供postman使用(最low的方式)
var date = new Date(); date.setDate(date.getDate() + 10); var year = date.getFullYear().toString().s ...