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

Note:

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

给出二叉树的中序遍历和后序遍历结果,恢复出二叉树。

后序遍历序列的最后一个元素值是二叉树的根节点的值。查找该元素在中序遍历序列中的位置mid,依据中序遍历和后序遍历性质。有:

位置mid曾经的序列部分为二叉树根节点左子树中序遍历的结果,得出该序列的长度n,则后序遍历序列前n个元素为二叉树根节点左子树后序遍历的结果。由这两个中序遍历和后序遍历子序列恢复出左子树。

位置mid以后的序列部分为二叉树根节点右子树中序遍历的结果,得出该序列的长度m,则后序遍历序列(除去最后一个元素)后m个元素为二叉树根节点右子树后序遍历的结果,由这两个中序遍历和后序遍历子序列恢复出左子树;

以上描写叙述中递归地引用了由中序遍历和后序遍历恢复子树的部分,因此程序也採用递归实现。

AC code:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ TreeNode *helper(vector<int> &inorder, int b1, int e1, vector<int> &postorder, int b2, int e2)
{
if (b1>e1)
return NULL;
int mid;
for (int i = b1; i <= e1; i++)
if (inorder[i] == postorder[e2])
{
mid = i;
break;
} TreeNode* root = new TreeNode(inorder[mid]);
root->left = helper(inorder, b1, mid - 1, postorder, b2, b2 + mid - b1-1);
root->right = helper(inorder, mid + 1, e1, postorder, b2 + mid-b1, e2 - 1);
return root;
}
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
return helper(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
}
};

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

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

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

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

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

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

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

随机推荐

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"的作用

    为页面添加正确的DOCTYPE 很多设计师和开发者都不知道什么是DOCTYPE,DOCTYPE有什么用.DOCTYPE是document type的简写.主要用来说明你用的XHTML或者HTML是什么 ...

  2. Windows 10开发基础——文件、文件夹和库(一)

    原文:Windows 10开发基础--文件.文件夹和库(一) 主要内容: 1.枚举查询文件和文件夹 2.文本文件读写的三种方法——创建写入和读取文件 3.获得文件的属性 枚举查询文件和文件夹 先了解一 ...

  3. Entity Framework学习笔记

    原文地址:http://www.cnblogs.com/frankofgdc/p/3600090.html Entity Framework学习笔记——错误汇总   之前的小项目做完了,到了总结经验和 ...

  4. REST Web Server,REST介绍

    参考资料: 1.http://www.chinalivedoor.com/story/1123.html 2. Backbone.js 是一种重量级javascript  MVC 应用框架,通过Mod ...

  5. pagination分页插件

    最近做了个项目,有用到分页, 这类插件应该是很常用的, 虽然网上很多现成的分页插件, 但是还是想着自己写一个, 给自己积累点东西, 顺便练练手, 写了差不多3个小时左右, 代码如下: 代码: < ...

  6. 将类数组对象(array-like object)转化为数组对象(Array object)

    用法:Array.prototype.slice.call(array-like object) // 创建一个类数组对象 var alo = {0:"a", 1:"b& ...

  7. 第9课_3_db库安装

    四 安装listener监听 使用oracle用户,在rac1和rac2上都可以起netca 1.打开网络配置界面,选择"Cluster configuration"集群配置方案, ...

  8. 【转】sun.misc.BASE64Encoder找不到jar包的解决方法

    只需要在project build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了.(太神奇了,转自http://blog. ...

  9. 20151214--JSTL

  10. jsp文件中的路径问题

    最近在写一个OA系统,在资源的路径问题上面出现了一点问题,使用相对路径的话不利于文件的改动,所以使用了绝对路径来写. 在jsp文件中   <%= String path = request.ge ...