PAT_A1138#Postorder Traversal
Source:
Description:
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 (≤ 50,000), 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
Keys:
- 二叉树的遍历
Attention:
- 建树的过程实质上也是遍历二叉树的过程
Code:
/*
Data: 2019-05-26 20:53:20
Problem: PAT_A1138#Postorder Traversal
AC: 16:20 题目大意:
假设二叉树中各个结点权值不同且为正;
给出先序和中序遍历,求后序遍历中的第一个结点
*/ #include<cstdio>
const int M=1e5;
int pre[M],in[M],f=; void Travel(int preL, int preR, int inL, int inR)
{
if(preL > preR)
return;
int k;
for(k=inL; k<=inR; k++)
if(in[k]==pre[preL])
break;
int numLeft = k-inL;
Travel(preL+, preL+numLeft, inL,k-);
Travel(preL+numLeft+, preR, k+,inR);
if(f){
printf("%d\n", in[k]);f=;
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &pre[i]);
for(int i=; i<n; i++)
scanf("%d", &in[i]);
Travel(,n-,,n-); return ;
}
PAT_A1138#Postorder Traversal的更多相关文章
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Leetcode 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 ...
- Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ 106. 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 ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
随机推荐
- hibernate之多对多映射
目录 第一章 多对多的应用场景 第二章 多对多的映射配置案例 2-1 创建项目和表 2-2 创建持久化类和映射文件 2-3 配置映射文件 2-4 测试 第三章 总结 源码地址:https://gith ...
- 2.3-STP生成树
2.3-STP生成树 单点失效(signle point of failure)及其解决方法: 当两个Segement之间只有一条物理连接时就有可能出现单点失效→ 单方面的故障导致全网 ...
- 转:java身份证格式强校验
package com.dsh.zealandweb.utils; import java.util.HashSet; import java.util.regex.Pattern; import o ...
- Java 判断中文字符
Java判断一个字符串中是否有中文字符有两种方法,但是原理都一样,就是通过Unicode编码来判断,因为中文在Unicode中的编码区间为:0x4e00--0x9fa5 第一种: String chi ...
- DockPanelSuite中的DocumentStyle
首先明确一个概念Mdi的完整词组:Multiple document interface namespace WeifenLuo.WinFormsUI.Docking { public enum Do ...
- SQL排他锁的解决方案
SQL排他锁的解决方案 分类: 事务与锁 数据库管理维护2009-04-28 22:41 680人阅读 评论(0) 收藏 举报 sql数据库database服务器killdisk 问题描述: 我有一个 ...
- python nltk 入门demo
sudo pip install -U pyyaml nltk import nltk nltk.download() 搞不定,必须代理: Installing via a proxy web ser ...
- 10.3.3 WebView的几个常见功能
(1)背景设置,例如下面的代码. webView.setBackgroundColor(0);//先设置背景色为transparent webView.setBackgroundResource(R. ...
- unity3D 使用欧拉角控制视野注意点
变量声明: public PlayerInput p; //表示控制代码用来获得用户是否按下 public float rotateSpeed = 50f; //旋转速度 private GameOb ...
- 解决VS2008 开发Wince应用程序项目生成速度慢的问题
最近用VS2008开发Windows Mobile程序,使用C#..NET Compact Framework,发现项目生成速度比较慢.用VS2008打开项目后,开始一段时间生成速度还能忍受,时间一长 ...