Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the first number of the postorder traversal sequence of the corresponding binary tree.

Sample Input:

7
1 2 3 4 5 6 7
2 3 1 5 4 7 6

Sample Output:

3

后序遍历的顺序是先左右再树根,那么就一层一层来如果左子树不为空,那么答案一定在左子树,否则如果右子树不为空,那么答案一定在右子树,如果都不行,那么答案就是树根。
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int q[],z[];
int n;
int findpostfirst(int q1,int q2,int z1,int z2)
{
for(int i = z1;i <= z2;i ++)
{
if(z[i] == q[q1])
{
if(i != z1)return findpostfirst(q1+,q1+i-z1,z1,i-);
else if(i != z2) return findpostfirst(q1+i-z1+,q2,i+,z2);
else return q[q1];
}
}
}
int main()
{
cin>>n;
for(int i = ;i < n;i ++)
{
cin>>q[i];
}
for(int i = ;i < n;i ++)
{
cin>>z[i];
}
cout<<findpostfirst(,n-,,n-);
}

1138. Postorder Traversal (25)的更多相关文章

  1. PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...

  2. PAT 1138 Postorder Traversal [比较]

    1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  3. PAT 甲级 1138 Postorder Traversal

    https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...

  4. PAT 1138 Postorder Traversal

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  5. 1138 Postorder Traversal

    题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值. 思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图.本题并不是让我们输出后序序列 ...

  6. PAT_A1138#Postorder Traversal

    Source: PAT A1138 Postorder Traversal (25 分) Description: Suppose that all the keys in a binary tree ...

  7. PAT A1138 Postorder Traversal (25 分)——大树的遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  8. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

  9. 【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的 ...

随机推荐

  1. FIFO设计验证经验谈

    概述: FIFO是电路设计中非常重要的一个基本电路.一般的超大规模集成电路中,都会用到FIFO.所以,FIFO是每个SOC设计和验证工程师必须掌握的一种核心电路. FIFO电路又分为异步FIFO和同步 ...

  2. Luogu-3250 [HNOI2016]网络

    Luogu-3250 [HNOI2016]网络 题面 Luogu-3250 题解 CDQ分治...这个应该算是整体二分吧 二分重要度,按照时间从小到大加入大于重要度的边 对于一个询问,如果经过这个点的 ...

  3. java+opencv+intellij idea实现人脸识别

    首先当然是需要安装opencv了,我用的是opencv2.4.13.下载完之后就可以直接安装了,安装过程也很简单,直接下一步下一步就好,我就不上图了. 接下来在opencv下找到jar包,比如我直接安 ...

  4. android studio 慢的问题

    转自:http://www.jianshu.com/p/e1c0b46e317c 在使用Android studio的时候常常遇到这样的问题,从github或是其他地方导入项目,Android stu ...

  5. 平滑重启php

    kill -USR2 `cat /usr/local/webserver/php/var/run/php-fpm.pid`

  6. MapReduce job在JobTracker初始化源码级分析

    mapreduce job提交流程源码级分析(三)中已经说明用户最终调用JobTracker.submitJob方法来向JobTracker提交作业.而这个方法的核心提交方法是JobTracker.a ...

  7. 缓存技术内部交流_01_Ehcache3简介

    参考资料: http://www.ehcache.org/documentation/3.2/getting-started.html http://www.ehcache.org/documenta ...

  8. StringUtils在commons-lang3和commons-lang中的区别【转】

    http://blog.csdn.net/eden_m516/article/details/75042439 最近经常需要对String做一些判断和处理,于是就用到了Apache提供的StringU ...

  9. B/S,C/S简单介绍

    B/S,C/S 架构 硬件环境不同:C/S 一般建立在专用的网络上, 小范围里的网络环境, 局域网之间再通过专门服务器提供连接和数据交换服务. B/S 建立在广域网之上的, 不必是专门的网络硬件环境, ...

  10. POI使用总结

    一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.二. HSSF概况 HSSF 是H ...