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. 【META http-equiv="Content-Type" Content="text/html; Charset=*】意义详解

    [META http-equiv="Content-Type" Content="text/html; Charset=*]意义详解 META,网页Html语言里Head ...

  2. 20145201《Java程序设计》第五次实验报告

    实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 我负责客户端 组队队员:鄢曼君20145227负责服务器 博客地址:htt ...

  3. 20145109 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 S.O.L.I.D原则: ...

  4. iOS日常学习 - 让你的 Xcode8 继续使用插件

    本文转载,原文链接 随着 iOS10 的正式版即将发布,Xcode8 GM 也在发布会后放出,本文不会涉及到 Xcode8 有哪些更新,而是记录了如何让 Xcode8 继续支持 Plugin. 相信各 ...

  5. Eclipse 启动tomcat 访问主页报错404

    问题 tomcat用startup.sh启动,访问localhost:8080能正常访问,用Eclipse service启动tomcat,访问localhost:8080报错404 解决方法 1. ...

  6. jvm-内存区域与内存溢出异常

    内存区域与内存溢出异常 参考: http://www.cnblogs.com/ityouknow/p/5610232.html 所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢? ...

  7. springBean集合注入的方法

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  8. 线段树 - HDU1166 - 敌兵布阵

    2017-07-29 16:41:00 writer:pprp 线段树跟区间操作相关,想要在题目限定的时间内解决问题就需要用线段树这种数据结构来解决: 线段树是一种二叉平衡树 参考书目:张新华的< ...

  9. 4.微信小程序-B站:先把首页造出来

    (小安娜:失踪人口已上线,大家快来喷喷喷他!),sorry++,最近身边发生太多事情,导致这最关键的实战开篇都未写,(小安娜-分身1:懒就是懒,不负责任,我之前学的都忘了),(小安娜-分身2:上一篇双 ...

  10. geoserver源码maven编译相关问题

    1.登陆失败跳转404错误 登陆失败后指向的路径为: http://192.168.15.97:8080/hgisserver/web/wicket/bookmarkable/org.geoserve ...