一.题目

Construct Binary Tree from Inorder and Postorder Traversal

Total Accepted: 33418 Total Submissions: 124726My
Submissions

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss










二.解题技巧

    这道题和Construct Binary Tree from Preorder and Inorder Traversal类似,都是考察基本概念的,后序遍历是先遍历左子树。然后遍历右子树,最后遍历根节点。

    做法都是先依据后序遍历的概念,找到后序遍历最后的一个值。即为根节点的值,然后依据根节点将中序遍历的结果分成左子树和右子树。然后就能够递归的实现了。
    上述做法的时间复杂度为O(n^2),空间复杂度为O(1)。
    


三.实现代码

#include <iostream>
#include <algorithm>
#include <vector> /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ using std::vector;
using std::find; struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution
{
private:
TreeNode* buildTree(vector<int>::iterator PostBegin, vector<int>::iterator PostEnd,
vector<int>::iterator InBegin, vector<int>::iterator InEnd)
{
if (InBegin == InEnd)
{
return NULL;
} if (PostBegin == PostEnd)
{
return NULL;
} int HeadValue = *(--PostEnd);
TreeNode *HeadNode = new TreeNode(HeadValue); vector<int>::iterator LeftEnd = find(InBegin, InEnd, HeadValue);
if (LeftEnd != InEnd)
{
HeadNode->left = buildTree(PostBegin, PostBegin + (LeftEnd - InBegin),
InBegin, LeftEnd);
} HeadNode->right = buildTree(PostBegin + (LeftEnd - InBegin), PostEnd,
LeftEnd + 1, InEnd); return HeadNode;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
if (inorder.empty())
{
return NULL;
} return buildTree(postorder.begin(), postorder.end(), inorder.begin(),
inorder.end()); }
};



四.体会

   这道题主要考察的是基本概念。并没有非常复杂的算法在里面,能够算是对于二叉树的遍历的进一步理解。




版权全部,欢迎转载。转载请注明出处,谢谢




LeetCode_Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. Construct Binary Tree from Inorder and Postorder Traversal

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

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

  3. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  4. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  5. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

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

  6. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

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

  7. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  8. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  9. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

随机推荐

  1. ActiveMQ学习笔记(15)----Message Dispatch高级特性(一)

    1. Message Cursors 1.1 概述 ActiveMQ发送持久化消息的典型的厝里方式是:当消息的消费者准备就绪时,消息发送系统把存储的消息按批次发送给消费者,在发送完一个批次的消息后,指 ...

  2. Uncaught TypeError: Cannot read property 'offsetTop' of undefined at VueComponent.handleScroll

    mounted() { window.addEventListener("scroll", this.handleScroll); }, beforeDestroy() { win ...

  3. C语言静态库与动态库(Windows下测试)

    转载于:https://zhidao.baidu.com/question/1946953913764139388.html,原文为Linux上测试,本文为在Windows上编译测试 我们通常把一些公 ...

  4. csv 模块的基本使用

    csv 模块专门用于读取和写入 csv 文件内容 以下主要讲在 python2 中的使用,在python3中有不同的地方,我会单独指出来 一般的excel表格可以保存为csv格式,然后就可以使用 cs ...

  5. thttpd 在S3C6410的移植-web服务程序的应用

    1.    在VMWare 虚拟机上将arm-linux-gcc 4.3.1配置好:2.    下载thttpd软件包并解压:3.    在thttpd根目录下运行:  ./configure:4.  ...

  6. Json相关内容

    一. 导入包:net.sf.json.JSONObject 代码 import net.sf.json.JSON; import net.sf.json.JSONArray; import net.s ...

  7. Java的TreeMap,C++的lower_bound,合并间隔

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/?tab=Description 这道题目是合并间隔的经典题目. htt ...

  8. delphi网络函数大全

    {=========================================================================功 能: 网络函数库时 间: 2002/10/02版 ...

  9. 51nod-1322: 关于树的函数

    [传送门:51nod-1322] 简要题意: 给出n个点的两棵无根树,编号都是从0到n-1 现在每棵树任意选出一条边割断,设第一棵树选出的边为e1,第二棵树选出的边为e2 很显然割断后两棵树各分成了四 ...

  10. 远程登录工具 —— filezilla(FTP vs. SFTP)、xshell、secureCRT

    filezilla:是一个免费开源的 FTP 软件,分为客户端版本和服务器版本,具备所有的 FTP 软件功能. 支持的协议:FTP & SFTP(Secure File Transfer Pr ...