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

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


类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html

只是子树的前序和中序遍历序列分别更新为:

//左子树:
left_prestart = prestart+1
left_preend = prestart+index-instart
//右子树
right_prestart = prestart+index-instart+1
right_preend = preend

代码如下:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int InorderIndex(int[] inorder,int key){
if(inorder == null || inorder.length == 0)
return -1; for(int i = 0;i < inorder.length;i++)
if(inorder[i] == key)
return i; return -1;
}
public TreeNode buildTreeRec(int[] preoder,int[] inorder,int prestart,int preend,int instart,int inend){
if(instart > inend)
return null;
TreeNode root = new TreeNode(preoder[prestart]);
int index = InorderIndex(inorder, root.val);
root.left = buildTreeRec(preoder, inorder, prestart+1, prestart+index-instart, instart, index-1);
root.right = buildTreeRec(preoder, inorder, prestart+index-instart+1, preend, index+1, inend); return root;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeRec(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
}
}

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

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

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

  3. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

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

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

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

  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. Construct Binary Tree from Preorder and Inorder Traversal

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

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

  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. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

随机推荐

  1. Django开发之html交互

    html中用户输入信息,由Django的view.py处理,大致用到了以下几类格式: 1. 文本框 <input type="text" name="vid&quo ...

  2. centos7.0 安装nginx

    在centos7.0下安装nginx需要安装 prce和zlib包去官网下载相应的包 然后解压相应的包进行编译 解压nginx源码包进入到解压文件 ./configure --sbin-path=/u ...

  3. 【BZOJ5037】[Jsoi2014]电信网络 最大权闭合图

    [BZOJ5037][Jsoi2014]电信网络 Description JYY创建的电信公司,垄断着整个JSOI王国的电信网络.JYY在JSOI王国里建造了很多的通信基站.目前所有的基站都是使用2G ...

  4. eclipse同一个工作空间下分开多个项目

    在Package Explorer顶部的右侧有有机表图标按钮,点击倒三角 Top Level Elements->Working Set.此时就会发现,很多项目会自动纳入一个文件夹,这个文件夹的 ...

  5. Dubbo,ZooKeeper,Redis,FastDFS,ActiveMQ,Keepalived,Nginx,Hudson

    获取[下载地址]   QQ: 313596790   [免费支持更新] 三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体 [新录针对本系统的视频教程,手 ...

  6. JVM指令重排

    指令重排的基本原则: a.程序顺序原则:一个线程内保证语义的串行性 b.volatile规则:volatile变量的写,先发生于读 c.锁规则:解锁(unlock)必然发生在随后的加锁(lock)前 ...

  7. Django开发模式会加载两次settings文件导致RotatingFileHandlerError

    当使用RotatingFileHandler作为django的日志处理器的时候,会报: Traceback (most recent call last): File "C:\Python2 ...

  8. 检出的maven项目没有“M”标识

    从SVN服务器上检出的项目,在项目左上角没有“M”标识 解决此方案有两种方法: 1.将检出的项目“删除”(不删除工作空间的项目),重新导入,选择“Maven 项目”,导入完成即可. 2.选中项目右键, ...

  9. 提供的STC89C52RC单片机GPS在LCD1602液晶只显示welcome to use问题?

    1.使用USB-TTL接GPS单独测试GPS定位在所处环境看是否能定位到. 2.检查自己使用的单片机是不是STC89C52RC型号,提供例程使用的这个型号单片机.如果使用其他51单片机,请先使用STC ...

  10. 20145229吴姗珊 《Java程序设计》小总结

    20145229吴姗珊 <Java程序设计>小总结 教材学习内容总结 由于今天考试考到了操作题,根本无从下手,然后才意识到原来之前的学习都是蜻蜓点水,一味的把学习建立在给老师学,为家长学的 ...