题目

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. 六十一、SAP中的逻辑运算与进制转换

    一.代码如下 二.16进制计算过程如下 三.计算结果为16进制的11,也就是10进制的17

  2. postman测试带有json数据格式的字段

    测试六个字段 普通字段: ModelCode 普通字段: MmodelCode 普通字段: ModelTagKey 普通字段: ModelTagValue 普通字段: ModelTagType jso ...

  3. enlipse 快捷键

    ctrl+shift+o  去掉多余的引用类

  4. 第二十篇ORM查询与SQL语句

    ORM查询与SQL语句 多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情 ...

  5. vue学习(八)nextTick[异步更新队列]的使用和应用

    nextTick的使用 为了数据变化之后等待vue完成更新DOM,可以在数据变化之后立即使用Vue.nextTick()在当前的回调函数中能获取最新的DOM <div id="app& ...

  6. 本地登录ftp的时候报530错误

    root@instance-iyi104bj:~# ftp localhost Connected to localhost. (vsFTPd ) Name (localhost:root): roo ...

  7. 关于GAN的一些笔记

    目录 1 Divergence 1.1 Kullback–Leibler divergence 1.2 Jensen–Shannon divergence 1.3 Wasserstein distan ...

  8. one_day_one_linuxCmd---crontab 命令

    <坚持每天学习一个 linux 命令,今天我们来学习 tar 命令> 摘要:crond 是 linux 下用来周期性的执行某种任务或等待处理事件的一个守护进程,周期执行的任务一般由 cro ...

  9. C 的printf函数

    头文件 #include <stdio.h> printf函数是最常用的格式化输出函数,原型为:int printf(char *format,......); printf函数会根据参数 ...

  10. UML-设计模式-缓存策略

    继续前一章<本地服务容错> 问题:考虑加载内存ProductCatalog缓存和基于LocalProducts文件缓存的可选方案: 一种是惰性初始化(lazy init):当实际读取外部产 ...