1138. Postorder Traversal (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 (<=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)的更多相关文章
- PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1138 Postorder Traversal
https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...
- PAT 1138 Postorder Traversal
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 1138 Postorder Traversal
题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值. 思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图.本题并不是让我们输出后序序列 ...
- PAT_A1138#Postorder Traversal
Source: PAT A1138 Postorder Traversal (25 分) Description: Suppose that all the keys in a binary tree ...
- PAT A1138 Postorder Traversal (25 分)——大树的遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】
我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列, ======O=========== 后序遍 ...
- 【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的 ...
随机推荐
- Linux下的查找命令which、whereis、locate、find(6/20)
Linux下查找相关命令主要有以下4个:which.whereis.locate.find. (1)which [-a] cmdname1 cmdname2 ...... 命令参数: -n ...
- 一般处理程序中 C#中对象转化为Json对象
namespace: Newtonsoft.Json; context.Response.ContentType = "application/text"; 注:这里为什么不是 J ...
- NET中IL指令详解
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...
- C++ string 用法总结
string查找替换.分割字符串.比较.截取.类型转换.排序等功能都提供了强大的处理函数,可以代替字符数组来使用. 熟练掌握好string的各种使用方法,能极大的提高编程效率哦 ^_^. #inclu ...
- 2 Powershell与Cmd以及Unix/Linux Shell
上篇文章我说道,windows为了改变用户对其console界面的诟病,于是就从windows vista开始,计划要改变这种局面,于是就有 了Powershell的出现. 1.兼容shell命令 ...
- JMS-activeMq发布订阅模式
上一篇对点对点模式进行总结,这一篇都发布订阅模式进行总结,代码都差不多,唯一区别就是创建队(session.createQueue(目的地))列改为创建主题(session.createTopic(目 ...
- python技术
要把zabbix弄成自动监控,下发任务,部署,事件恢复得功能
- Python中有趣的数据结构
链表 链表的基本操作 >>> a = [66.25,333,333,1,1234.5] >>> print a.count(333),a.count(66.25), ...
- 使用springmvc的时候报错NoSuchBeanDefinitionException: No qualifying bean of type
NoSuchBeanDefinitionException: No qualifying bean of type 其实我至今都不知道错误的根源在哪里,<context:component-sc ...
- SDRAM引入
SDRAM:Synchronous Dynamic Random Access Memory,同步动态随机存储器. DDR: DDR是DDR SDRAM,是SDRAM的升级版.(DDR:double ...