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

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

代码:

#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. ps(笔记)

    窗口 工作区 默认窗口(恢复) ctrl + n 点阵图(像素图) 小方格组成的 alt 键 配合 放大缩小 ppi dpi 打印输出. 画布新建 z键 局部放大 右击实际像素操作 f键 全屏 空格键 ...

  2. python学习笔记(excel+unittest)

    准备先利用之前整理的python自带的unittest框架 整合excel 实现接口自动化测试功能 先看看excel表格设置: 下来是对excel获取的代码: #!/usr/bin/env pytho ...

  3. PL/SQL通过修改配置文件的方式实现数据库的连接

    http://jingyan.baidu.com/article/c74d600080632a0f6a595d80.html

  4. java String转Long两种方法区别

    第一种:包装类型:Byte,Integer,Short,Long,Boolean,Character,Float,Double等8种 Long.valueOf("String")返 ...

  5. Workflow Builder 2.6.3 Certified on Windows 10 for EBS 12.x

    By Steven Chan - EBS-Oracle on May 17, 2016 Workflow Builder 2.6.3 is now certified on Windows 10 de ...

  6. windows钩​子​

    (转自:http://wenku.baidu.com/view/5d41fdbec77da26925c5b08d.html) Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消 ...

  7. 【ccf 2017/12/4】行车路线(dijkstra变形)

    问题描述 小明和小芳出去乡村玩,小明负责开车,小芳来导航. 小芳将可能的道路分为大道和小道.大道比较好走,每走1公里小明会增加1的疲劳度.小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走s公 ...

  8. eclipse 不能启动虚拟机

    在eclipse文件下有个eclipse.ini文件打开它.现在如果没有-vm 这个参数那么就在最后新起一行写上 -vm+空格+参数 参数是你安装的jdk目录比如我的 -vm d:/java/jdk1 ...

  9. 使用js构造"ddMMMyy"格式的日期供postman使用(最low的方式)

    var date = new Date(); date.setDate(date.getDate() + 10); var year = date.getFullYear().toString().s ...

  10. PS基础教程[6]如何快速制作一寸照片

    一寸照片使我们经常会用到的,很多的证件照都是使用一寸的照片作为存档的.写这个经验也是因为刚刚有网友求助做一寸照片,所以就顺便写个经验.废话不多说了,进入正题,PS基础教程之快速制作一寸的照片. 制作方 ...