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

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

class Solution {
public:
TreeNode *buildTree(vector<int>& inorder, int in_left,int in_right,
vector<int>& postorder, int post_left, int post_right){
if(in_right < in_left || post_right < post_left) return NULL;
TreeNode *root = new TreeNode(postorder[post_right]);
int index = in_left;
for( ; index <= in_right; ++ index ) if(inorder[index] == postorder[post_right]) break;
int right_cnt = in_right-index;
root->left = buildTree(inorder,in_left,index-,postorder,post_left,post_right-right_cnt-);
root->right = buildTree(inorder,index+,in_right,postorder, post_right-right_cnt,post_right-);
return root;
} TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
if(inorder.size() == ) return NULL;
else return buildTree(inorder,,inorder.size()-, postorder,,postorder.size()-);
}
};

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

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

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

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

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

  4. LeetCode——Construct Binary Tree from Inorder and Postorder Traversal

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

  5. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

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

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

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

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

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

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

  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. 加载plist文件数据的方法

    这个pilist文件最外面的是一个数组,数组中每一个item是一个字典,我们的目的就是为了取到每一个item字典中的内容数据 下面看代码举例 //加载数组 - (void)handleData { / ...

  2. 快速傅里叶(FFT)的快速深度思考

    关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...

  3. IT 外包中的甲方乙方,德国人,美国人,印度人和日本人印象杂谈

    开篇介绍 最近经常和朋友聚会,三十而立的年龄自然讨论最多的就是各自的小家庭,如何赚钱,工作,未来的就业发展,职业转型等话题.还有各种跳槽,机会选择,甲方乙方以及外包中的各种趣事,外企与国内私企的发展机 ...

  4. MFC在关闭第二个窗口时关闭主对话框

    AfxGetApp()->m_pMainWnd->SendMessage(WM_CLOSE);//关闭主对话框

  5. js判断本地是否安装app

    var ua = navigator.userAgent.toLowerCase(); 1.判断是否是微信 function isWeixinBrowser() { return (/micromes ...

  6. HeapSort 堆排序 基于伪代码实现

    此文原创, http://www.cnblogs.com/baokang/p/4735431.html ,禁止转载 GIF 动态图 伪代码 /* From Wikipedia, the free en ...

  7. rcnn学习(六):imdb.py学习

    # -------------------------------------------------------- # Fast R-CNN # Copyright (c) 2015 Microso ...

  8. npm start 作用

    在配置phonecat项目时需要运行npm start在本地配置一个服务器环境,npm start首先会安装一系列的必要程序,这些程序依赖package.json中的内容,package.json中的 ...

  9. NOIp2014 解题报告

    有史以来第一届面向社会征题的NOIp结束了.最开始以为面向社会征题会很难,但是这是我参加的最水的一次NOIp了. 由于停了两月的课,所以现在正在补文化科目就没时间打代码了.所以所有的题目就均不给出代码 ...

  10. css的用法

    Css(Cascading Style Sheets,层叠样式表)是一种页面美化方法,通过编辑Css的对象属性达到美化页面的效果.Css的操作基本单元为对象,使用CSS的感觉就像是使用C++/C中的函 ...