题目:

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

思路:

后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可。

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} inorder
* @param {number[]} postorder
* @return {TreeNode}
*/
var buildTree = function(inorder, postorder) {
if(postorder.length==0){
return null;
}
return BuildTree(0,inorder.length-1,0,postorder.length-1,inorder,postorder);
}; function BuildTree(iStart,iEnd,pStart,pEnd,inorder,postorder){
if(pStart==pEnd){
return new TreeNode(postorder[pEnd]);
}
if(iStart>iEnd){
return null;
}
var rootval=postorder[pEnd];
var i=inorder.indexOf(rootval);
var root=new TreeNode(rootval);
root.left=BuildTree(iStart,i-1,pStart,pStart+(i-iStart)-1,inorder,postorder);
root.right=BuildTree(i+1,iEnd,pStart+(i-iStart),pEnd-1,inorder,postorder);
return root;
}

【树】Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

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

  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 Week14]Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

  5. Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

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

  10. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

随机推荐

  1. springMVC学习(注解实现依赖注入)

    原文:http://blog.csdn.net/mockingbirds/article/details/45399691 上一篇博客,学习了spring的依赖注入,即利用spring容器来为类中的属 ...

  2. PBOCIC读芯片卡流程

    https://blog.csdn.net/kxd_ysheng/article/details/21178101?_t=t PBOCIC读芯片卡流程,参考上面的博客,整理了一下PBOCIC卡读流程. ...

  3. IDEA14/Eclipse+Tomcat7热部署,jrebel6破解与eclipse配置

    换了最新的eclipse,以前很多的插件都用不了,对于web开发的人来说,jrebel这种防重启神器必须要配备,防止修改类名.java文件.配置文件后的tomcat重启. 首先给一个下载地址: htt ...

  4. 安卓添加USB外置UVC摄像头

    实现的方法有很多种,按步骤来看适合哪一种,网上说什么接采集卡,其实就是把AV转成UVC,现在市面上很多摄像头直接就已经是UVC的了,在windows上面即插即用. 安卓也是Linux,这个就好办了. ...

  5. Nodejs-- web服务器

    第一篇关于nodejs的东西,代码在此作为备份. 该代码目前未完成,是一个阻塞式的代码. 1.index.js ar server=require('./server'); var route=req ...

  6. MySql采用range分区可提升查询效率

    简介: RANGE分区基于一个给定的连续区间范围,早期版本RANGE主要是基于整数的分区.在5.7版本中DATE.DATETIME列也可以使用RANGE分区,同时在5.5以上的版本提供了基于非整形的R ...

  7. 如何在 .NET Core 上测试库

    设置解决方案. 可使用以下命令实现此目的: mkdir SolutionWithSrcAndTest cd SolutionWithSrcAndTest dotnet new sln dotnet n ...

  8. cmd命令使用笔记

    使用资源管理器打开当前路径 explorer %cd%

  9. C++实现排序算法

    稳定性:快速 希尔 选择 堆排序不稳定 时间复杂度:平均情况下,快速.希尔.归并和堆排序的时间复杂度均为O(nlog2(n)),其他都是O(n^2).最坏情况下,快排的时间复杂度为O(n^2) #in ...

  10. 689. Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...