题意:给出二叉树的前序序列后中序序列,输出其后序序列的第一个值。

思路:乍一看不就是前序+中序重建二叉树,然后后序遍历嘛!这么做当然不会有错,但是却没有真正领会本题的意图。本题并不是让我们输出后序序列,而只是输出后序序列的第一个值!考虑到后序遍历的顺序是:左子树->右子树->根结点,思考一下,如果根结点存在左子树,那么后序序列的第一个值肯定在左子树中,这个时候右子树就不是我们关心的了;如果没有左子树,那么后序序列的第一个值就在右子树中。因此,我们只需要在常规的“构建二叉树”的函数中递归找到这个值就可以了,而不需要真的去重建二叉树。这么做代码量就少了很多。

代码:

#include <cstdio>
;
int pre[maxn],in[maxn];
int n;
int func(int preL,int preR,int inL,int inR)
{
    if(preL==preR) return pre[preL];//边界
    int pos=inL;
    while(in[pos]!=pre[preL]) pos++;
    int leftCnt=pos-inL;//左子树的结点个数
    ,preL+leftCnt,inL,pos-);//如果左子树存在,往左子树递归
    ,preR,pos+,inR);//左子树不存在,往右子树递归
}

int main()
{
    scanf("%d",&n);
    ;i<n;i++) scanf("%d",&pre[i]);
    ;i<n;i++) scanf("%d",&in[i]);
    printf(,n-,,n-));
    ;
}

1138 Postorder Traversal的更多相关文章

  1. PAT 1138 Postorder Traversal [比较]

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

  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 1138 Postorder Traversal

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

  5. PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]

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

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

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

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

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

  9. Binary Tree Postorder Traversal

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

随机推荐

  1. JNI简单步骤01

    1.环境变量 1.1.相应的环境变量中,加入如下内容:(Windows) (1).ClASSPATH中输入 : ".;C:\Program Files\Java\jdk1.7.0_07\jr ...

  2. python学习笔记(matplotlib下载安装)

    最近博主在找工作换新环境.昨天电话面试中问到python中threading模块进行接口性能测试的时候.如何生成性能测试报告 我现在还停留在打印在屏幕中.所以今天想着是否可以生成相应的性能测试报告 首 ...

  3. python twisted教程[资料]

    python twisted教程 一,异步编程 http://www.douban.com/note/232200511/   python twisted教程 二:缓慢的诗 http://www.d ...

  4. language model ——tensorflow 之RNN

    代码结构 tf的代码看多了之后就知道其实官方代码的这个结构并不好: graph的构建和训练部分放在了一个文件中,至少也应该分开成model.py和train.py两个文件,model.py中只有一个P ...

  5. netcat 瑞士军刀

    netcat被誉为网络安全界的‘瑞士军刀’,一个简单而有用的工具,透过使用TCP或UDP协议的网络连接去读写数据.它被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强 ...

  6. kafka系列之(3)——Coordinator与offset管理和Consumer Rebalance

    from:http://www.jianshu.com/p/5aa8776868bb kafka系列之(3)——Coordinator与offset管理和Consumer Rebalance 时之结绳 ...

  7. Mybatis与Hibernate的详细对比

    前言 这篇博文我们重点分析一下Mybatis与hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...

  8. 51nod 1085 背包问题

    在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数).求背包能够容纳的最大价值. 收起   输入 第1行,2个 ...

  9. 重温CLR(八 ) 泛型

    熟悉面向对象编程的开发人员都深谙面向对象的好处,其中一个好处是代码重用,它极大提高了开发效率.也就是说,可以派生出一个类,让他继承基类的所有能力.派生类只需要重写虚方法,或添加一些新方法,就可定制派生 ...

  10. 【LeetCode】汇总

    此贴为汇总贴 673. Number of Longest Increasing Subsequence 075. Sort Colors 009. Palindrome Number 008. St ...