【树】Construct Binary Tree from Preorder and Inorder Traversal
题目:
Given preorder and inorder 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[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function(preorder, inorder) {
if(preorder.length==0){
return null;
}
return BuildTree(0,preorder.length-1,0,inorder.length-1,preorder,inorder);
}; function BuildTree(pStart,pEnd,iStart,iEnd,preorder,inorder){
if(pStart==pEnd){
return new TreeNode(preorder[pStart]);
}
if(pStart>pEnd){
return null;
}
var rootval=preorder[pStart];
var i=inorder.indexOf(rootval);
var root=new TreeNode(rootval);
root.left=BuildTree(pStart+1,pStart+(i-iStart),iStart,i-1,preorder,inorder);
root.right=BuildTree(pStart+1+(i-iStart),pEnd,i+1,iEnd,preorder,inorder);
return root;
}
【树】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【题解二连发】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 ...
- 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: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal根据前中序数组恢复出原来的树
[抄题]: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume ...
随机推荐
- 4) Maven 安装
# ----------------------------------------------------------------------------# Maven2 Start Up Batc ...
- Spring源码解析 - AbstractBeanFactory 实现接口与父类分析
我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ...
- C语言中union关键字
union 关键字的用法与struct 的用法非常类似. union 维护足够的空间来置放多个数据成员中的“一种”,而不是为每一个数据成员配置空间,在union 中所有的数据成员共用一个空间,同一时间 ...
- Watermelon -- codeforces
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93241#problem/A (654123) http://codeforces.co ...
- 转:解决windows下eclipse中android项目关联android library project失败问题
近日,在做一个人人的第三方小项目.打算直接使用renren 的sdk 进行开发.因为renren的sdk是以android library project 形式发布的(关于这种project的内容可以 ...
- Google Summer of Code礼包
这个暑假参加google summer of code, 给Google的分布式容器管理系统kubernates开发新的特性,希望从中学习更多的分布式的技术,锻炼自己的编程技巧. 中午在学校的图书馆吗 ...
- 百分之 95% 的程序员不知道 Trending 是什么。
前言如果学习到的知识不成体系,那么遇到问题时就会非常难解决.常有人问你从哪里了解新技术怎么判断其发展趋势的,除了关注 Hacker News 以及庞大的 Awesome 还有没有其它方式?有啊当然是每 ...
- 译:Microsoft/ReactXP 简介
在Github的Microsoft项目中发现一个名为ReactXP的项目,这是一个由Skype团队开发的,用于进行Web及跨平台APP开发的库(建立在React Js 和 ReactNative之上) ...
- 微软在线实验室启用谷歌的reCAPTCHA,我们又丢失了一个好东东
在没有启用reCAPTCHA的日子,我们可以在微软的在线实验室www.microsoft.com/handsonlabs 中找到许许多多的文档.视频.动手实验环境. 不需要任何硬件.技术,就可以快速的 ...
- Team Foundation Server (TFS)集成Flyway,实现数据库的版本管理
1 概述 在系统开发过程中,我们对软件源代码的版本管理,已经有了比较成熟的解决方案.通过使用TFVC或GIT等源代码管理工具,可以非常方便的对软件代码实现回退.比较.分支合并等版本操作.对于软件依赖的 ...