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】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的 ...
随机推荐
- 基于Bootstrap的日历控件和日期选择插件
在线演示 本地下载
- HTML5抽奖转盘
在线演示 本地下载
- 利用SSH协议在Windows下使用PuTTY连接Ubuntu
利用SSH协议在Windows下使用PuTTY连接Ubuntu Ubuntu部分 首先我们要为Ubuntu配置一下环境,让它支持ssh服务,我们要做的其实也很简单,就一下两步: 安装OpenSSH软件 ...
- 配置windows qt开发环境
1.解压缩MinGW-gcc440_1.zip.将解压后的文件夹复制至C盘根目录下.2.安装qt-creator-win-opensource-2.5.2.3.安装qt-win-opensource- ...
- Kotlin 取 MAC 地址
package com.example.ybs.myapplication import android.annotation.SuppressLint import android.net.wifi ...
- IDEA 编译时 未结束的字符串文字
这个问题就是编码的问题,修改文件的编码可以解决 1. IDEA中 file-->Settings 找到File Encodings,将IDE Encoding.Project Encodin ...
- 关系型数据库的ACID规则
1.A (Atomicity) 原子性 原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的条件是事务里的所有操作都成功,只要有一个操作失败,整个事务就失败,需要回滚. 比如 ...
- No module named yum
升级python之后,执行yum的时候可能出现错误,类似: There was a problem importing one of the Python modulesrequired to run ...
- crm开发(基于ssh)(1)
搭建crm练习ssh环境 第一步 导入jar包 第二步 搭建struts2环境 (1)创建action,创建struts.xml配置文件,配置action (2)配置struts2的过滤器 第三步 搭 ...
- CentOS下安装mysql及配置使用
最近一直使用的是CentOS,平时用的最多的数据库是Sql Server,对于mysql还停留在上学的时候,早已忘得一干二净,写这篇内容目的是,重新学习如何安装使用mysql. 一.安装mysql 操 ...