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 ...
随机推荐
- istringstream 用法
istringstream 类用于执行C++风格的串流的输入操作 istringstream用空格作为字符串分隔符 #include <iostream>#include <sstr ...
- aps.net session全面介绍(生命周期,超时时间)
Asp.Net中的Session与Cookie最大的区别在于:Cookie信息全部存放于客户端,Session则只是将一个ID存放在客户端做为与服务端验证的标记,而真正的数据都是放在服务端的内存之中的 ...
- poj3517约瑟夫问题
直接套公式+ 假设除去第k个人. 0, 1, 2, 3, ..., k-2, k-1, k, ..., n-1 //original sequence (1) 0, 1, 2, 3, ..., k-2 ...
- Map类集合
集合类 Key Value ...
- time模块详解
本文转自这里: 在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: 在Python中,通常有 ...
- 【spark】RDD创建
首先我们要建立 sparkconf 配置文件,然后通过配置文件来建立sparkcontext. import org.apache.spark._ object MyRdd { def main(ar ...
- [JS学习笔记]Event对象
写在前面 学习和总结JS时会伴随性的生成一些dome,其中包含一些动态输出的结果和标注. 之前通过鸡贼的办法实现了在博客中执行JS,但很多时候需要一张干净的页面编写dome,所以尝试通过一些在线的JS ...
- [置顶]
Android玩转百度地图Sha1获取正确姿势?
场景一 由于最近项目钟要用到定位功能因此肯定需要用到地图以及地位功能,相信大家也知道目前国内比较出名的地图像百度.高德.腾讯等这些还是用到比较多的,于是思考了一下决定还是用百度,相信老司机们都知道的哈 ...
- canvas 创建颜色渐变柱状图
最终结果: 代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...
- js错误Cannot set property 'action' of null
Cannot set property 'action' of null [自己解决问题答案] 应该放到form里面 [网上答案]是页面无法加载完毕执行代码.可以把获取元素等一系列的操作放在 wind ...