【树】Construct Binary Tree from Inorder and Postorder Traversal
题目:
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的更多相关文章
- 【题解二连发】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 ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
- 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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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: ...
- 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 ...
随机推荐
- ACM STEPS——Chapter Two——Section One
数学题小关,做得很悲剧,有几道题要查数学书... 记下几道有价值的题吧 The area(hdoj 1071) http://acm.hdu.edu.cn/showproblem.php?pid=10 ...
- Java 连接 Memcached 服务
原文:http://www.runoob.com/memcached/java-memcached.html mac下安装和配置Memcached:http://www.pchou.info/open ...
- [译]window.onerror事件
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- TFlearn——(2)SVHN
1,数据集简介 SVHN(Street View House Number)Dateset 来源于谷歌街景门牌号码,原生的数据集1也就是官网的 Format 1 是一些原始的未经处理的彩色图片,如下图 ...
- 配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)
生成本地log最好生成多个文件放在一个文件夹里,特别多的时候一个小时一个文件 配置好Nginx后,通过flume收集日志到hdfs 可参考flume的文件 用flume的案例二 执行的注意点 avro ...
- 作为CTO如何做技术升级
升级技术架构,先要革新观念,最后才是技术问题 升级技术架构,不仅仅是技术升级 说到升级架构,大家第一个都会想到,是不是对技术升级一下就可以了? 我认为不是,技术架构升级要求的是整个公司的升级. 技术架 ...
- web.xml文件中context-param、listener、filter、servlet的执行顺序
首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> ...
- 团队作业第四周(HCL盐酸队)——项目冲刺(第一篇)
任务认领情况: 1.坦克类实现:李密,卢泰佑 2.子弹类,线程类实现:黄国航 赖少勇 3.画笔类,地图的实现:陈舒标 黄宇航 明日任务安排: 今天在通过已经购买的GUI书籍的帮助下,已经实现了界面的 ...
- Time&Patience
“时间和耐心,是世间最强大的两个勇士.” “对未来越有信心,对今天越有耐心,坚持做正确的事.” 每日必做清单 每天六组俯卧撑 活在当下 接收脆弱 一万小时理论(罗马不是一天建成的,胖 ...
- Delphi XE4 For IOS中程序的调试(虚拟机,真实机和win32)
安装完之后,大家可以看一下XE4可以新建的工程类型: File->New: 是不是多出了FireMonkey Mobile Application这一个选项呀! 然后你再点击这个菜单项,弹出Fi ...