题目

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

注意:

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

例如,给出

中序遍历 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. ubuntu下搭建android开发环境核心篇安装AndroidStudio、sdk、jdk

    本文系转载http://blog.csdn.net/lsyz0021/article/details/52215996 一.安装前的准备 1.1.如果你还没有安装ubuntu 14.04 LTS系统, ...

  2. My first Python program(附增加清屏方法)

    #TempConvert.py TempStr = input("请输入带有符号的温度值:") if TempStr[-1] in ['F', 'f']: C = (eval(Te ...

  3. [Java][Servlet] Cannot call sendRedirect() after the response has been committed

    做一个Login Demo的时候,写了如下代码: protected void doPost(HttpServletRequest request, HttpServletResponse respo ...

  4. JavaSE之Java基础(1)

    1.为什么重写equals还要重写hashcode 首先equals与hashcode间的关系是这样的: 1.如果两个对象相同(即用equals比较返回true),那么它们的hashCode值一定要相 ...

  5. 在GitHub,他们是怎么玩的? (转)

    文章来源: http://www.cocoachina.com/gamedev/misc/2014/0530/8616.html   Github.com,现在是全世界程序员,尤其是开源爱好者的乐园. ...

  6. Wallet address

    BCX XZVYYwXFAJwv6x4KTssQxJb4EReVdCBnpb BCD 1DNSFUD7LURZdmbckkQcxMvinNJ26mVcNH

  7. IO文件操作

    × 目录 [1]IO文件的操作 [2]Directory类 [3]File类 [4]FileStream类 [5]文本文件的操作 一.IO文件的操作:   .net中对文件操作,经常会用到这样几个类: ...

  8. manjaro安装后你需要做的配置

    1.切换中国源 sudo gedit /etc/pacman-mirrors.conf 如果提示没有gedit , 则执行命令 : sudo pacman -S gedit 修改如下地方为中国: On ...

  9. python JSON性能测试与simplejson对比

    简单测试了一下,如果用JSON,也就是python2.6以上自带的json处理库,效率还算可以: 1K的数据,2.9GHz的CPU,单核下每秒能dump:36898次.大约是pyamf的5倍.但数据量 ...

  10. 如何让Oracle数据库保持优良性能的方法

    OracleDatabase,又名OracleRDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.它是在数据库领域一直处于领先地位的产品.可以说Oracle数据库系统是目前世界上流行 ...