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 ...
随机推荐
- jq-demo-楼梯效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- scala中的闭包
scala闭包 代码示例: package test.close_pack import scala.collection.mutable.ArrayBuffer /** * AUTHOR Guozy ...
- Nginx的动静分离
Nginx的动静分离 在之前我们的负载均衡中,我们再jsp中设置了一个背景,这是一个静态资源,Tomcat处理静态资源的效率并没有Nginx高,我们可以通过动静分离将静态资源和动态资源分割开来,Tom ...
- kafka的消息组件
kafka的组件的介绍 produer:消息的生产者,往topic当中生产消息 consumer:消息的消费者,从topic当中消费消息 broker:kafka的服务器 zookeeper:kafk ...
- mac 安装并使用 mysql 或者 mac mysql 忘记密码,Can't connect to local MySQL server through socket homebrew
1. brew install mysql 2. 启动mysql mysql.server start 我遇到了这个error,查openstack解决,我在这粘一下 ### Error:Can't ...
- DLL劫持技术例子: HijackDll
控制台程序:DllLoader Dll加载器,用于动态加载目标Dll,并动态调用目标函数 #include <cstdio> #include <windows.h> type ...
- elasticsearch的基本用法(转载)
本文出自:http://blog.csdn.net/feelig/article/details/8499614 最大的特点: 1. 数据库的 database, 就是 index 2. 数据库 ...
- Function(高阶函数式编程)
Function一个可以进行高阶函数式编程的模块. chain def chain[a](fs: Seq[(a) ? a]): (a) ? a 把一些列的方法串起来,挨个执行,每个方法的结果,回作为下 ...
- 逻辑回归,多分类推广算法softmax回归中
转自http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 简介 在本节中,我们介绍Softmax回归模型,该模型是log ...
- 使Excel中单元格内英文为Arial Narrow 中文为宋体显示打印
因为在对数据表进行排版格式整理的时候,发现Arial Narrow字体是不支持中文的,所以中文默认为宋体,但是显示出来却不是宋体,需要双击单元格中文才显示为宋体,这样打印出来才为宋体 但是如果有很多单 ...