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

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

代码:

#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. GDI+ 双缓冲字体模糊

    只是记录自己的UI库,对其他估计没什么帮助 void CListCtrlUI::ReFillRect(HDC hdc){ if (!m_pImage) { Graphics gs(hdc); int ...

  2. JAVA 多线程轮流打印ABC

    采用Thread+Semaphore实现,思路很简单 import java.io.IOException; import java.util.concurrent.Semaphore; public ...

  3. IDL语言开发规范

    一.支持的类型 1.IDL支持常见的基本类型,常量,枚举,容器,结构体,服务.不支持多态和重载,参数.返回值不能为空,各个基本类型的标识如下: bool:对应java的boolean,布尔类型(tru ...

  4. 【spark】常用转换操作:join

    join就表示内连接. 对于内链接,对于给定的两个输入数据集(k,v1)和(k,v2) 根据相同的k进行连接,最终得到(k,(v1,v2))的数据集. 示例 val arr1 = Array((&qu ...

  5. css权威指南读书笔记

    今天翻手机,翻到了许久之前看css权威指南时的笔记,遂移到博客中来. 1.属性选择器p.one class名为one的p元素p[class][name] 含有class和name属性的p元素p[cla ...

  6. eclipse 智能提示js和jquery等前端插件

    使用Eclipse写Jquery和Javascript代码的时候,是没有智能提示的.我们可以使用一个插件来解决这个问题. 安装完成后,Eclipse会自动重启.重启之后,我们在项目上右键,   根据自 ...

  7. LEX下出毛病的问题

    毛病! 1.今日写词法分析,回想起第一次写时候的蓝色警告:不要随便管理员.so,便Win+R,"cmd",回车. 2.在用lex写的时候,注意注释是 /*注释放于此处*/ 而非一般 ...

  8. del语句

    5.2. del 语句 有个方法可以从列表中按给定的索引而不是值来删除一个子项: del 语句.它不同于有返回值的 pop() 方法.语句 del 还可以从列表中删除切片或清空整个列表(我们以前介绍过 ...

  9. 使用Gson轻松解决复杂结构的Json数据解析

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50961803 JSON简介 JSON(JavaScript Object Notati ...

  10. UVA136 Ugly Numbers

    题意 PDF 分析 用堆和集合维护即可. 时间复杂度\(O(1500 \log n)\) 代码 #include<iostream> #include<cstdio> #inc ...