Source:

PAT A1138 Postorder Traversal (25 分)

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的更多相关文章

  1. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  2. [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 ...

  3. 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 ...

  4. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  5. 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 ...

  6. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  7. Leetcode Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  8. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  9. 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 ...

随机推荐

  1. [bzoj3872][Poi2014]Ant colony_树形dp

    Ant colony bzoj-3872 Poi-2014 题目大意:说不明白.....题目链接 注释:略. 想法:两个思路都行. 反正我们就是要求出每个叶子节点到根节点的每个路径权值积. 可以将边做 ...

  2. N天学习一个linux命令之rsync

    用途 主要用于本地和远程主机同步文件 特性 1 使用增量传输算法(delta-transfer algorithm) 2 支持ssh,rsync协议 3 可以用于本地同步文件 4 本地和远程主机都需要 ...

  3. 臣妾做不到啊---记团队内首次尝试HOLACRACY管理会议

    "易穷则变,变则通.通则久". --<系辞传>下篇的第二章 实践敏捷方法接近两年,瓶颈效应逐渐显现.计穷力竭之时.有幸了解到HOLACRACY.购买了市面上唯一一本中文 ...

  4. Zend_Form 创建、校验和解析表单的基础--(手冊)

    1.  创建表单对象 创建表单对象很easy:仅仅要实现 Zend_Form: <?php $form = newZend_Form; ? > 对于高级用例.须要创建 Zend_Form ...

  5. SolrCloud 分布式集群部署步骤

    https://segmentfault.com/a/1190000000595712 SolrCloud 分布式集群部署步骤 solr solrcloud zookeeper apache-tomc ...

  6. IOS6.0自带下拉刷新控件UIRefreshControl

    1.UIRefreshControl必需要在IOS6.0以后才干使用,同一时候他仅仅能在UITableViewController类中才干够使用 2.使用比較简单 self.refreshContro ...

  7. 使用Memcached改进Java企业级应用性能:架构和设置

    Memcached由Danga Interactive开发.用来提升LiveJournal.com站点性能. Memcached分布式架构支持众多的社交网络应用,Twitter.Facebook还有W ...

  8. Codeforces Round #332 (Div. 2)C. Day at the Beach 树状数组

    C. Day at the Beach   One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortuna ...

  9. jQuery总结04

    1 JavaScript 中的 AJAX 的四个实现步骤分别是? 2 如何处理 XMLHttpRequest 对象的兼容问题? 3 jQuery 中的 AJAX 4 jQuery 选择器包括哪些? 5 ...

  10. Microsoft ASP.NET SignalR

    SignalR类似与JavaScript实时框架,如Socket.IO.SignalR能够完成客户端向服务器的异步通信,并同时支持服务器向浏览器客户端推送事件.SignalR的连接通过日益流行的Web ...