一.题目

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. ZBrush中Zproject与SubTool的综合应用

    在ZBrush中有很多工具也需要配合子物体工具来使用,如笔刷中的Zproject(投射笔刷)就需要子物体工具来配合使用,本文将讲解一下它的用法. 1. 首先创建一个平面,在Tool(工具)面板中选择P ...

  2. BZOJ 5394 [Ynoi2016]炸脖龙 (线段树+拓展欧拉定理)

    题目大意:给你一个序列,需要支持区间修改,以及查询一段区间$a_{i}^{a_{i+1}^{a_{i+2}...}}mod\;p$的值,每次询问的$p$的值不同 对于区间修改,由线段树完成,没什么好说 ...

  3. Mean, Median, Mode, Range, and Standard Deviation

    Descriptive statistics tell you about the distribution of data points in data set. The most common m ...

  4. 二叉排序树(B-Tree)-c实现

    这个二叉排序树写完了,虽然还有些bug,但还是很高兴的. 主要实现二叉排序树的构建.(*表示稍微重要点) 二叉排序树的打印. 二叉排序树的删除. 代码里的三种情况都测了 顺便附送一个简单的中序遍历,递 ...

  5. ElementUi rules表单验证

    ElementUi 表单验证 工作中常用到的JS验证 可以在pattern中书写正则,并且配合elementUI进行表单验证. pattern 属性规定用于验证输入字段的模式.模式指的是正则表达式. ...

  6. django 用户上传文件media的存储访问配置1

    1. 首先新建文件夹media  后 在项目setting中具体配置: MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media ...

  7. Python求阴影部分面积

    一.前言说明 今天看到微信群里一道六年级数学题,如下图,求阴影部分面积 看起来似乎并不是很难,可是博主添加各种辅助线,写各种方法都没出来,不得已而改用写Python代码来求面积了 二.思路介绍 1.用 ...

  8. HTTP——学习笔记(3)

    HTTP报文:用于HTTP协议交互的信息,客户端的HTTP报文叫做 请求报文,响应端的叫做 响应报文 本质:是由多行(用CR+LF作换行符)数据构成的字符串文本 注:CR:回车,打印针回到行首   L ...

  9. QTP11.5公布,改名UFT

    QTP11.5公布,改名UFT,支持多脚本编辑调试.PDF检查点.持续集成系统.手机測试等 http://www.learnqtp.com/hp-uft11-5-qtp-new-features/ T ...

  10. 【金阳光測试】基于控件核心技术探讨---Android自己主动化系列(2)---2013年5月

    第一讲分享了下安卓自己主动化一些概况和一些自己主动化框架现状和技术可以解决什么样的问题. 这次课就深入到android世界里面.遨游.翱翔.深入了解自己主动化測试核心技术. 搞过编程开发的同学听到in ...