题目:

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的更多相关文章

  1. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

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

  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. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

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

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

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

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

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

随机推荐

  1. (动态规划 最长有序子序列)Monkey and Banana --HDU --1069

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1069 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. [转] libcurl异步方式使用总结(附流程图)

    文为转载,原文地址:libcurl异步方式使用总结 实习期间用到了libcurl来做HTTPS双向认证,用的是异步方式,简单总结一下. libcurl这个库的同步方式很简单,不做介绍,而异步方式很难理 ...

  3. 从数据库到NoSQL思路整理

    1. 数据库为什么要算范式?细说起来太多. 范式解决了数据冗余,从而保证ACID的操作性能.不然一堆删除异常,插入异常,就没法愉快的写SQL了 另外,对于多个业务公用的数据库,范式解决了集成的问题. ...

  4. [Linux]几个armhf的ubuntu源

    摘自百度贴吧,留存 http://mirrors.ustc.edu.cn/ubuntu-ports/http://ftp.ubuntu-tw.org/mirror/ubuntu-ports/http: ...

  5. Tomcat监听443端口的方法

    当我们需要更安全的访问网站的时候就会选择使用https协议,而https协议默认的端口号为443端口,这就是我们为什么向让Tomcat监听在443端口的原因,因为监控在非80端口和443端口的web服 ...

  6. Tomcat跨域访问配置

    下载cors-filter-1.7.jar,java-property-utils-1.9.jar这两个库文件,放到Tomcat的lib目录下. D:\Program Files\Tomcat-7.0 ...

  7. EF添加Msysql实体异常:表“TableDetails”中列“IsPrimaryKey”的值为 DBNull。 ---> System.InvalidCastException: 指定的转换无效。

    尝试一下以下步骤: 1.关闭VS项目, 以管理员权限来打开: 1.执行语句 set global optimizer_switch='derived_merge=OFF'; set optimizer ...

  8. Django:如何给文章列表添加图片

    思路: 使用ajax方式将图片和文本一起通过formData提交到后台,Django后台通过request.POST和request.FILES方式接收数据 1.前端代码 {% extends 'ba ...

  9. jQuery-Load方法

    1.load() 介绍:load() 方法通过 AJAX 请求从服务器加载数据,并把返回的数据放置到指定的元素中 该方法是最简单的从服务器获取数据的方法.它几乎与 $.get(url, data, s ...

  10. 【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】

    游戏开发中常遇到资源保护的问题. 目前游戏开发中常加密的文件类型有:图片,Lua文件,音频等文件,而其实加密也是一把双刃剑. 需要安全那就得耗费一定的资源去实现它.目前网上也有用TexturePack ...