题目

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:

你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]

返回如下的二叉树:

    3
/ \
9 20
/ \
15 7

Tag

dfs . post+inorder .递归。二分查找


代码

还是跟105一样的思路。注意mid此时是post的最后一个。并且递归时,先递归右子树,再递归左子树。因为是后序遍历。

//recursive .dfs
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
int mid= postorder.size()-1 ;//后序遍历。根节点在最后一个。
return Helper(inorder,postorder,mid,0,postorder.size()-1);
} TreeNode* Helper(vector<int>& inorder,vector<int>& postorder,int & mid ,int start ,int end )
{
if(start>end||mid<0)
return nullptr; TreeNode* root=new TreeNode(postorder[mid]);
auto pos = distance(inorder.begin(),find(inorder.begin()+start,inorder.begin()+end,postorder[mid]) );
mid--;//从后面找根节点 //后序是 先递归右子树。再递归左子树
root->right=Helper(inorder,postorder,mid,pos+1,end);
root->left=Helper(inorder,postorder,mid,start,pos-1);
return root;
}
};

问题

LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树

    根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15, ...

  2. Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. PHP速学

    基本代码 <?php echo "Hello world";?> 变量定义 <?php $a=true; $bool_value=true; $integer_v ...

  2. angularJS ui router 多视图单独刷新问题

    场景:视图层级如下 view1 --view11 --view111 需求:view11的一个动作过后,单独刷新view12 解决方式:修改层级设计 view1 --view11 --view111 ...

  3. C#中事件的一些总结

    事件中  sender和e  事件中不同的对象 object  sender  是事件的对象 eventages e   是事件对象传递过来的参数对象 如 private button_click(O ...

  4. 初次搭建spring boot 项目(实验楼-学习笔记)

    首先说一下springboot 的优点: 使用Spring Initializr可以在几秒钟就配置好一个Spring Boot应用. 对大量的框架都可以无缝集成,基本不需要配置或者很少的配置就可以运行 ...

  5. Linux 拷贝

    拷贝文件夹下所有内容到另一个文件夹: cp -rf 源文件 目标文件 例如:cp -rf /home/efs/Desktop/WEB-INF/* /opt/IBM/WebSphere/AppServe ...

  6. 微信小程序电商实战-首页(上)

    嗨,大家好!经过近两周的精心准备终于开始微信小程序电商实战之路喽.那么最终会做成什么样呢?当然可以肯定不会只做一个静态demo哦,先把我们小程序电商实战的整体架构发出来晒一下,请看下图:   架构图. ...

  7. vue组件双向绑定.sync修饰符的一个坑

    我们知道组件是单项的,但是有时候需要双向,这时候我们可以使用.sync修饰符,但今天遇到一个坑,一直不成功,花了半小时试出来的.... 在编程的时候我们很习惯冒号后面跟着空格.而.sync双向绑定需要 ...

  8. css3骰子(transform初识)

    利用css3制作可旋转的骰子,效果图如下,也可以查看 demo: 首先是骰子的html结构,.page 是骰子的六个页面的 class,#one-#six分别表示六个面,.point 是骰子表面的点数 ...

  9. Quick-Cocos2d-x Lua脚本加密打包器

    准备开新项目了,在寻找合适的框架,后来就发现了Quick-Cocos2d-x这玩意. 别说,还挺好使.之后一步步研究,发现Lua不加密是不行的. 加密的方法在这里. 因为在做版本更新的时候,一般大家都 ...

  10. EF--DB First

    DB First先有数据库,根据数据库生成Model实体对象. 1.新建数据库表,Poet,Poem,Meter.关系如下: 建表语句 create table Poet ( PoetId ,) pr ...