PAT Advanced 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
题目分析
已知前序和中序,求后序第一个节点值
解题思路
- 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) [树的遍历,前序中序转后序]的更多相关文章
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT 甲级 1138 Postorder Traversal
https://pintia.cn/problem-sets/994805342720868352/problems/994805345078067200 Suppose that all the k ...
- 1138. Postorder Traversal (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- 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 ...
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- 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 ...
- 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 ...
- 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】
我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列, ======O=========== 后序遍 ...
- [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 ...
随机推荐
- Java多线程涉及的概念(3)
Java多线程涉及的概念 在理解进程和线程概念之前首选要对并发有一定的感性认识,如果服务器同一时间内只能服务于一个客户端,其他客户端都再那里傻等的话,可见其性能的低下估计会被客户骂出翔来,因此并发编程 ...
- 065-PHP函数中声明全局变量
<?php function test(){ //定义函数 global $a; //声明全局变量 $a=7; echo "函数内: ".$a . "<br& ...
- winfrom窗体的透明度
在VS中创建一个Winform项目,其默认的窗体名称为 Form1. 在VS设计界面中对 Form1 的 Opacity 属性值设置为 50%. 没错,就这样就可以了. 方法2: ...
- spring源码 BeanFactory根接口
/* * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Vers ...
- Vue核心知识一览
生命周期 beforeCreate : 数据观测 和 初始化事件还未开始 created : ...
- P 1033 旧键盘打字
转跳点:
- JS最新最细面试题
转之:https://www.jianshu.com/p/f1f39d5b2a2e 1. javascript的typeof返回哪些数据类型. 答案:string,boolean,number,und ...
- JS - 解决引入 js 文件无效的问题
增加 type 即可 <script type="text/javascript" src="....js"></script>
- io流对数据的读写
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...
- Callable、Future、线程池简单使用
Callable.Future与线程池 在创建新线程的三种方式中,继承Thread和实现Runnable接口两种方式都都没有返回值,因此当我们想要获取子线程计算结果时只能设置共享数据,同时还需要考虑同 ...