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 (≤ 50,000), 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 <iostream>
#include <vector>
using namespace std;
int n, num;
bool flag = false;
vector<int>preOrder, inOrder, postOrder;
void travel(int root, int L, int R)//L,R为中序遍历的,root为前序遍历的第一个点
{
if (L > R || flag)
return;
int i = L;
while (inOrder[i] != preOrder[root])++i;
travel(root + , L, i - );//遍历左子树Order
travel(root + + i - L, i + , R);//遍历左子树Order
//postOrder.push_back(preOrder[root]);//得到完整的后序遍历
if (!flag)//输出一个就剪枝
{
cout << preOrder[root] << endl;
flag = true;
}
}
int main()
{
cin >> n;
for (int i = ; i < n; ++i)
{
cin >> num;
preOrder.push_back(num);
}
for (int i = ; i < n; ++i)
{
cin >> num;
inOrder.push_back(num);
}
travel(, , n - );
//cout << postOrder[0] << endl;
return ;
}

PAT甲级——A1138 Postorder Traversa【25】的更多相关文章

  1. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  2. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  3. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  4. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  5. PAT甲级 1121. Damn Single (25)

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...

  6. PAT甲级 1126. Eulerian Path (25)

    1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...

  7. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  8. PAT甲级 1129. Recommendation System (25)

    1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. PAT 甲级 1138 Postorder Traversal

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

随机推荐

  1. php Excel导出功能

    /** * * execl数据导出 */ function exportOrderExcel2($title, $cellName, $data) { //引入核心文件 vendor("PH ...

  2. Json数据交换一Gson

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. 添加依赖 <depe ...

  3. Java初识方法

    5.初识方法 方法就是一段代码片段,这个片段可以完成特定的功能,并且可以重复利用. 5.1 方法的定义 5.1.1方法的定义格式 [方法修饰列表] 返回值类型 方法名(方法参数列表){ 方法体 } ① ...

  4. Alibaba Cluster Data 开源:270GB 数据揭秘你不知道的阿里巴巴数据中心

    打开一篇篇 IT 技术文章,你总能够看到“大规模”.“海量请求”这些字眼.如今,这些功能强大的互联网应用,都运行在大规模数据中心上,然而,对于大规模数据中心,你又了解多少呢?实际上,除了阅读一些科技文 ...

  5. NXOpenC#_Training_2(cn)【转载】

  6. Annotation详解

    转自:http://www.doc88.com/p-995532241886.html 首先我们定义一个简单的注解 package com.qjy.annotation; import java.la ...

  7. 记录一次失败的向git提交代码,和解决的方法。(首次创建仓库)

    背景: 向git push代码(创建一个新的仓库) 做法: 在github创建一个新的仓库------>本地新建文件夹------->依次执行了下面的命令 git init git clo ...

  8. hexo next主题深度优化(三),引入require.js,适配pjax。

    文章目录 require.js的好处, hexo next中加入require.js 新建一个main.js作为所有js的入口 pjax的require.js实现 关于require js适配过程中报 ...

  9. jsp-解决自写Servlet老是报错404

    写好servlet进行测试老是报404解决方案. 1.确保web.xml配置好 2.Bulid Path项目,在Libraries界面Add External JARs,在tomcat的lib目录下面 ...

  10. CTF里的LSB