题意:根据二叉树的中序遍历和后序遍历恢复二叉树。

解题思路:看到树首先想到要用递归来解题。以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去。由于后序遍历的最后一个节点就是树的根。也就是root=1,然后我们在中序遍历中搜索1,可以看到中序遍历的第四个数是1,也就是root。根据中序遍历的定义,1左边的数{4,2,5}就是左子树的中序遍历,1右边的数{6,3,7}就是右子树的中序遍历。而对于后序遍历来讲,一定是先后序遍历完左子树,再后序遍历完右子树,最后遍历根。于是可以推出:{4,5,2}就是左子树的后序遍历,{6,3,7}就是右子树的后序遍历。而我们已经知道{4,2,5}就是左子树的中序遍历,{6,3,7}就是右子树的中序遍历。再进行递归就可以解决问题了。

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
if(inorder.size()!=postorder.size()||inorder.size()<)
return NULL;
return build(inorder,postorder,,inorder.size()-,,postorder.size()-);
}
TreeNode *build(vector<int> &inorder, vector<int> &postorder, int startin,int endin,int startpost,int endpost){
if(startin>endin||startpost>endpost) return NULL;
if(startin==endin) return new TreeNode(inorder[startin]);
TreeNode *res = new TreeNode(postorder[endpost]);
int tmp=postorder[endpost];
int index=;
while((startin+index)<=endin){
if(inorder[startin+index]==tmp)
break;
index++;
}
res->left=build(inorder,postorder,startin,startin+index-,startpost,startpost+index-);
res->right=build(inorder,postorder,startin+index+,endin,startpost+index,endpost-);
return res;
}
};

Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树的更多相关文章

  1. [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 ...

  2. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. [LeetCode] 106. 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 ...

  4. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)

    根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

  5. Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

  8. 【题解二连发】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 ...

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

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

随机推荐

  1. VMware RHEL6.3 开启网络连接

    确认/etc/sysconfig/network是否存在,如果不存在,service network 命令使用不了.新建: NETWORKING=yes HOSTNAME=RHEL6. GATEWAY ...

  2. javascript学习笔记 - 面向对象 理解对象

    ECMAScript 中有两种属性:数据属性和访问器属性 一 属性类型 1.数据属性.数据属性有4个描述其行为的特性 [[Configurable]]表示能否通过delete删除属性从而重新定义属性: ...

  3. 九度oj 题目1455:珍惜现在,感恩生活

    题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买.请问:你用有限的资金最 ...

  4. Python杂技

    py转exe文件 用 pyinstaller,可以把所有文件打包成一个单独的exe文件 win10X64 =>pip install pyinstaller pyinstaller [参数] [ ...

  5. iOS学习笔记43-Swift(三)类

    一.Swift的类class 作为一门面向对象语言,类也是Swift的非常重要的类型,我们先来看下一个简单的类 //Swift中一个类可以不继承于任何其他基类,那么此类本身就是一个基类 class P ...

  6. Codeforces963B - Destruction of a Tree

    Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...

  7. LineageOS源码定制手机系统

    LineageOS源码定制手机系统 导语: 现在市场的手机基本就两种:  苹果机和android机. 今天我们不谈苹果机, 对小编我来讲,那是个奢侈品, android是我的最爱.对于一般androi ...

  8. [转] Makefile 基础 (7) —— Makefile 中 make 的运行

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...

  9. 【前端学习笔记】2015-09-10~~~~ css层叠样式表~~格式

    关于颜色的定义: 有几种方式:1.直接接英文单词   2.#16进制  3.rgb(100%:0%:0%)或者里面写像素,此处百分号不能省略. 加多个属性时,用“;”分开 前面如果是定义标签的样式时, ...

  10. 怎样更改SQL Server 2008的身份验证方式(转)

    原文转自 http://blog.csdn.net/zjx86320/article/details/9745669         大家都知道sql server 有两种登录验证方式,即sql se ...