题目

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

题目分析

已知前序和中序,求后序第一个节点值

解题思路

  1. postFirst函数本质是建树过程,找到后序遍历的第一个节点后退出以节省时间(后序第一个节点为递归过程中第一个左子节点和右子节点都为NULL的节点)

易错点

该题节点数最多为50000个,若在找到后没有退出(即:完成建树过程)会超时

Code

Code 01

#include <iostream>
#include <vector>
const int maxn = 50000;
struct node {
int data;
node * left;
node * right;
};
int n,pre[maxn],in[maxn];
bool flag;
void postFirst(int inL,int inR, int preL,int preR) {
if(inL>inR||flag) return;
int k=inL;
while(k<inR&&in[k]!=pre[preL])k++;
postFirst(inL, k-1, preL+1, preL+(k-inL));
postFirst(k+1, inR, preL+(k-inL)+1, preR);
if(flag==false) {
printf("%d", pre[preL]);
flag=true;
}
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++)scanf("%d",&pre[i]);
for(int i=0; i<n; i++)scanf("%d",&in[i]);
postFirst(0,n-1,0,n-1);
}

PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]的更多相关文章

  1. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  2. PAT 甲级 1138 Postorder Traversal

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

  3. 1138. Postorder Traversal (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  4. PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]

    题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...

  5. PAT 1138 Postorder Traversal [比较]

    1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  6. PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]

    题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...

  7. PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟

    1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...

  8. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

  9. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. 2. FTP 服务器安装

    vsftp 安装(linux) Linux : 安装,创建虚拟用户,配置,防火墙设置 1. 安装 执行yum -y install vsftpd 注意: (1) 是否使用sudo权限执行请根据您具体环 ...

  2. servlet中调用注入spring管理的dao(转)

    今天做大型仪器的的时候遇到的问题,转下为了以后能用 http://blog.csdn.net/jiyingying_up/article/details/44803585 我们用spring的依赖注入 ...

  3. Golang go-gin 注册路由

    代码实现 main.go package main import ( "fmt" "github.com/jihite/go-gin-example/pkg/settin ...

  4. HDU 4866 多校1 主席树+扫描线

    终于是解决了这个题目了 不过不知道下一次碰到主席树到底做不做的出来,这个东西稍微难一点就不一定能做得出 离散化+扫描线式的建树,所以对于某个坐标二分找到对应的那颗主席树,即搜索出结果即可(因为是扫描线 ...

  5. .net微软企业库的事务回滚

    事务是自定义的一个操作序列. 其中的操作要么全部执行要么全部不执行,是一个不可分割的工作单位.通过事务,SQL Server能将逻辑相关的一组操作绑定在一起,以便服务器保持数据的完整性. Databa ...

  6. sql 左联 右联 内联的区别

    如有表a(col1,col2),a,1b,1 b(col1,col2)a,3c,2 内部联接是指只返回符合联接条件的资料,如select * from a join b on a.col1 = b.c ...

  7. 5 ~ express ~ 连接数据库

    1, 在schema 目录创建 users.js 文件,通过 mongoose 模块来操作数据库 2,  在定义 users 表结构之前,需要让应用支持或连接数据库 . 所以要在应用的入口文件 app ...

  8. tc: 模拟网络异常的工具

    作者:smallnest Linux Traffic Control (tc)的扩展 Network Emulation (netem)可以很方便的模拟网络不好的情况,一般新的linux内核中(> ...

  9. buildroot经验

    1.可以运行bulilroot下面的孙可编写的build.sh文件,自动配置和编译 2.如何添加要下载和编译的包? 如要下载和编译libevent, 可以通过make menuconfig, 然后搜索 ...

  10. 二、在SAP中创建一个程序

    一.我们来到SE38 二.添加一个程序的名字,需要以Y或者Z开头,点击创建就可以了 三.我们输入hello Sap,然后选择可执行程序,然后保存 四.创建对象目录时,可以选择把这个加入到包中,或者选择 ...