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

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

代码:

#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. java.util.logging.Logger_01

    1.参考网址 1.1.java.util.logging.Logger使用详解 http://lavasoft.blog.51cto.com/62575/184492 1.2.Java内置Logger ...

  2. Treflection04_面试题

    1. package reflectionZ; import java.lang.reflect.Field; import java.lang.reflect.Method; public clas ...

  3. vue组件化开发-vuex状态管理库

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具 ...

  4. Ajax-06 Ajax数据交换格式

    1.数据交换格式 服务端返回的数据,在本质上都是字符串,只是原生Ajax 或jQuery Ajax将这些字符串转换为容易理解的各种常用格式. a. Text 文本字符串 b.  XML JavaScr ...

  5. Rope的简单介绍

    repo的一些用法和理解 repo简单来讲就是一种调用git的一个脚本文件,用来管软件项目的软件仓库配置文件,描述了软件之间的一些依赖关系. 在Google的android项目下: repo只是goo ...

  6. Spring Boot打包总结

    环境配置信息 -** JDK 1.8 -** Spring Boot 1.5.3.RELEASE -** IDE: STS 3.4 Spring Boot下打包过程 基于STS创建Spring boo ...

  7. c++指针初探

    业余时间准备重温一下c++,因为在阅读Android源码到native层的时候感觉有点吃力,只是在大学时候很不用心的学过c++,所以重温下以便打好一些编程基础知识,本篇就很简单的对c++的指针做初步的 ...

  8. Golang 编译成 DLL 文件

    golang 编译 dll 过程中需要用到 gcc,所以先安装 MinGW. windows 64 位系统应下载 MinGW 的 64 位版本: https://sourceforge.net/pro ...

  9. 智能电视软件安装(WIFI上网)

    智能电视软件上网(电视可连接无线网的称之为:智能电视) 1.安装当贝市场 链接:http://www.dangbei.com/ 2.安装电视家浏览器 链接:http://www.tvapk.net/f ...

  10. 《C#求职宝典》读书笔记

    王小科 电子工业出版 第一篇 面试求职第一步 一个例子:一支行军中的队伍长100米,一个传令兵从队尾跑至队头,再立即返回队尾,队伍正好前进了100米.假设队伍 和传令兵行进的速度恒定,问传令兵跑了多少 ...