/*
* 105. Construct Binary Tree from Inorder and preorder Traversal
* 11.20 By Mingyang
* 千万不要以为root一定在中间那个点,还是要找一下中间点的位置
* p.left = construct(preorder, preStart + 1, preStart + (k - inStart),inorder, inStart, k - 1);
* p.right = construct(preorder, preStart + (k - inStart) + 1, preEnd,inorder, k + 1, inEnd);
* 难点就在于如何找到这两个起点和终点的index
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
int preEnd = preorder.length - 1;
int inEnd = inorder.length - 1;
return construct(preorder, 0, preEnd, inorder, 0, inEnd);
}
public TreeNode construct(int[] preorder, int preStart, int preEnd,int[] inorder, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) {
return null;
}
int val = preorder[preStart];
TreeNode p = new TreeNode(val);
// find parent element index from inorder
int k = 0;
for (int i = 0; i < inorder.length; i++) {
if (val == inorder[i]) {
k = i;
break;
}
}
p.left = construct(preorder, preStart + 1, preStart + (k - inStart),inorder, inStart, k - 1);
p.right = construct(preorder, preStart + (k - inStart) + 1, preEnd,inorder, k + 1, inEnd);
return p;
}

105. Construct Binary Tree from Inorder and preorder Traversal的更多相关文章

  1. leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal

    代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...

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

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

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

  5. Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

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

  6. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

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

  7. Construct Binary Tree from Inorder and Postorder Traversal

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

  8. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

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

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

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

随机推荐

  1. virtualbox没有64位选项

    今天安装的virtualbox想安装一下sql server 测试一下 在安装系统的时候发现没有64位系统的选项,在网上找了一下 发现是  在BIOS里面有一个选项没有开启, 是 Intel virt ...

  2. Django创建第一个应用

    一.创建第一个应用,并在settings.py中添加. python manage.py startapp article 二.创建第一个模型 class Article(models.Model): ...

  3. MongoDB最简单的入门教程之二 使用nodejs访问MongoDB

    在前一篇教程 MongoDB最简单的入门教程之一 环境搭建 里,我们已经完成了MongoDB的环境搭建. 在localhost:27017的服务器上,在数据库admin下面创建了一个名为person的 ...

  4. Python3简明教程(五)—— 流程控制之循环

    有些时候我们需要多次执行相同的任务,我们使用一个计数器来检查代码需要执行的次数.这个技术被称为循环. while循环 while语句的语法如下: while condition: statement1 ...

  5. 如何在一次请求中通过JS中获取Url中的参数

    从A跳转到B,携带参数 例如: /pc/B.jsp?item=123456 B页面在js可以直接用 var item='${param.item}'; 这样就拿到啦 还有一种方法 定义一个函数   f ...

  6. saltstack入门个人笔记

    offical website reference1 reference2 install apt-get install python-software-properties apt install ...

  7. 出现了错误。详细消息: 3 uncommitted changes would be overwritten by merge

    merge manual中有一条警告: 出现了错误.详细消息: 3 uncommitted changes would be overwritten by merge 有未提交修改情况下,不要执行me ...

  8. 安装Yii2提示Failed to decode response: zlib_decode(): data error错误解决方法

    如果是根据官方文档来安装(composer create-project --prefer-dist yiisoft/yii2-app-basic basic),并提示此错误的话,那么请做: 1. 请 ...

  9. C++关键字(保留字)

    C++ 关键字 点击下表以进入具体释义 __abstract 2 __alignof Operator __asm __assume __based __box 2 __cdecl __declspe ...

  10. Linux下安装Redis5.0.2

    1.下载redis 地址 http://download.redis.io/releases/redis-5.0.2.tar.gz 2.解压tar -zxf redis-5.0.2.tar.gz 3. ...