这道题是为数不多的感觉在读本科的时候见过的问题。

人工构造的过程是如何呢。兴许遍历最后一个节点一定是整棵树的根节点。从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右側的递归构造右子树。元素本身分配空间,作为根节点。

于set和map容器不同的是。vector容器不含find的成员函数。应该用stl的库函数,好在返回的也是迭代器,而vector的迭代器之间是能够做减法的。偏移量非常方便的得到。

TreeNode *buildRec(vector<int> &inorder, int si, vector<int> &postorder, int so, int len){
if(len <= 0) return NULL;
TreeNode *root = new TreeNode(postorder[so]);
int index = find(inorder.begin(), inorder.end(), postorder[so]) - inorder.begin();
int newlen = index - si;
root->left = buildRec(inorder, si, postorder, so-len+newlen, newlen);
root->right = buildRec(inorder, index+1, postorder, so-1, len-newlen-1);
return root;
} class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return buildRec(inorder, 0, postorder, inorder.size()-1, inorder.size());
}
};

leetcode第一刷_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】106. Construct Binary Tree from Inorder and Postorder Traversal

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

  3. LeetCode OJ 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. LeetCode OJ: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 ...

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

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

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...

  7. leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal

    构造方式跟中序与后序全然一样,并且一般都习惯正着来,所以更简单. 代码是之前写的,没实用库函数,不应该. TreeNode *buildIt(vector<int> &preord ...

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

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

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

随机推荐

  1. js处理json的方法

    var json = "{id:"myid", url:"http://www.myurl.com"}"; var js= (new Fun ...

  2. tomcat无法正常启动的一个原因

    简要报错信息: java.lang.IllegalArgumentException: Document base E:\apache-tomcat-7.0.65\webapps\springmvc0 ...

  3. SQL2-子查询、join查询

    SQL常用高级查询包括:Join查询.子查询. 子查询: USE flowershopdb --子查询:在一个select语句使用另一个select 语句作为条件或数据来源. --查询块:一个sele ...

  4. 星际SC地图制作中生成随机位置,也包括所有需要随机的效果

    星际SC地图制作中生成随机位置,也包括所有需要随机的效果 利用单位 kakaru T 开头那个, kakaru是随机变化位置 注意kakaru的放置位置和占用格子大小,kakaru周围放上LOCATI ...

  5. php phalcon

    1. 准备所需工具 1) php环境 浏览 2) phalcon插件 浏览 2. 安装phalcon 将php_phalcon.dll拷贝到%PHP_HOME%\ext目录中 修改php.ini文件, ...

  6. codeforces 632C. The Smallest String Concatenation 排序

    题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vec ...

  7. Linux下C编程通过宏定义打开和关闭调试信息

    GCC支持宏定义 gcc -Dmacro,将macro定义为1,我们可以利用这点在我们的代码中加入宏定义开关. #ifdef DEBUG #define pdebug(format, args...) ...

  8. discuz门户文章页面模板修改

    修改内容:view.htm 1.文章标题,模板代码 <h1 class="ph">$article[title] <!--{if $article['status ...

  9. c++ 静态多态与动态多态

    多态polymorphism是指具有多种形态的情况,它能根据单一的标记关联不同的行为.多态是面向对象程序设计的基础.在面向对象程序设计中的多态是一种运行时的多态.C++中有两种多态,称为动多态(运行时 ...

  10. 【Perl学习笔记】2. perl中的bless理解

    bless有两个参数:对象的引用.类的名称. 类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键. 所谓bless就是把 类型信息 赋予 实例变量. 程序包括5个文件:person.p ...