PAT甲级——A1138 Postorder Traversa【25】
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】的更多相关文章
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT甲级 1121. Damn Single (25)
1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT甲级 1129. Recommendation System (25)
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 甲级 1138 Postorder Traversal
https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...
随机推荐
- zepto-touch事件
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Java用集合实现斗地主发牌
本文以java双列集合HashMap为核心实现发牌操作: 思路: 1.准备牌:创建一个Map集合,存储牌的索引和组装好的牌 创建一个lis ...
- leetcode-126-单词接龙
题目描述: class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: list) -> list ...
- delphi xe10 网络文件传送
//网络传送文件(类似Server/Client) TTetheringManager|设备管理.TTetheringAppProfile|文件发送 待补充
- 字符串hash+回文树——hdu6599
拖了很久才补的回文树,感觉网上的博客都是一个做法..回文树统计不同种类的回文串出现次数,然后用字符串hash来判每个回文子串是否符合要求 #include<bits/stdc++.h> u ...
- 大转盘抽奖css3+js(简单书写)
今天花了一段时间简单写了下抽奖大转盘,这里写的只是自己想到的简单的写了下(也希望收获其他想法),后续,再写的话会更新. 大体思路:页面加载完成后,通过监听开始按钮的点击事件.然后会根据产生的随机数,通 ...
- NX二次开发-UFUN获取工程图详细信息UF_DRAW_ask_drawing_info
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_part.h> UF_initialize ...
- vue中使用router全局守卫实现页面拦截
一.背景 在vue项目中使用vue-router做页面跳转时,路由的方式有两种,一种是静态路由,另一种是动态路由.而要实现对路由的控制需要使用vuex和router全局守卫进行判断拦截(安全问题文章最 ...
- [转]gulp打包工具总结
与grunt类似,gulp也是构建工具,但相比于grunt的频繁IO操作,gulp的流操作能更快更便捷地完成构建工作.gulp借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级 ...
- 去除vim ^M符号
来自: https://zhidao.baidu.com/question/267905868.html 有两种情况,会出现打开的文件都显示^M符号:1. vim被配置为fileformat=unix ...