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

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

For example, given

preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]

Return the following binary tree:

    3
/ \
9 20
/ \
15 7
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
Map<Integer, Integer> mymap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
mymap.put(inorder[i], i);
}
return helper(0, inorder.length - 1, 0, preorder.length - 1, preorder, mymap);
} private TreeNode helper(int inLeft, int inRight, int preLeft, int preRight, int[] preorder, Map<Integer, Integer> mymap) {
if (inLeft > inRight) {
return null;
}
TreeNode cur = new TreeNode(preorder[preLeft]);
int leftSize = mymap.get(preorder[preLeft]) - inLeft;
// preRight for left just add up leftSize
cur.left = helper(inLeft, inLeft + leftSize - 1, preLeft + 1, preLeft + leftSize, preorder, mymap);
cur.right = helper(inLeft + leftSize + 1, inRight, preLeft + leftSize + 1, preRight, preorder, mymap);
return cur;
}
}
 
 

[LC] 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

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

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

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

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...

  6. LeetCode OJ 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 ...

  7. 【leetocde】 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 ...

  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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

随机推荐

  1. 从结构和数字看OO——面向对象设计与构造第一章总结

    不知不觉中,我已经接触OO五周了,顺利地完成了第一章节的学习,回顾三次编程作业,惊喜于自身在设计思路和编程习惯已有了一定的改变,下面我将从度量分析.自身Bug.互测和设计模式四个方向对自己第一章的学习 ...

  2. {转} 深入理解 HTTP Session

    session在web开发中是一个非常重要的概念,这个概念很抽象,很难定义,也是最让人迷惑的一个名词,也是最多被滥用的名字之一,在不同的场合,session一次的含义也很不相同.这里只探讨HTTP S ...

  3. ZOJ 3299 线段树 离散化

    本来是个很简单的题目,难住我的主要是这么几点 1.它所有的点都是坐标,不是实际的砖块,1,3指的是1-2 2-3的砖块...后来就是用1 代表1-2 ,2代表2-3.....,这样的话,每次读入的数据 ...

  4. 吴裕雄--天生自然MySQL学习笔记:MySQL 复制表

    如果需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等. 如果仅仅使用CREATE TABLE ... SELECT 命令,是无法实现的. 如何完整的复制MySQL数据表,步骤如下: 使用 ...

  5. SQL基础教程(第2版)第7章 集合运算:7-2 联结(以列为单位对表进行联结)

    第7章 集合运算:7-2 联结(以列为单位对表进行联结) ■联结的特定语法和过时语法 ● 联结( JOIN)就是将其他表中的列添加过来,进行“添加列”的集合运算.UNION是以行(纵向)为单位进行操作 ...

  6. nginx_tcp_proxy代理酸酸乳

    一.安装低版本的nginx(高版本不支持tcp代理模块:nginx_tcp_proxy_module)Nginx默认只支持http反向代理,要支持tcp反向代理,需在编译时增加tcp代理模块[ngin ...

  7. chrome安装switchyomega

    由于在国外网站找不到下载链接,在国内招了个crx文件,以下为安装crx教程 首先修改后缀为zip,再解压, 得到以下文件 然后在chrome里找到扩展程序, 打开开发者模式,点击-加载已解压的扩展程序 ...

  8. BP算法推导python实现

    def sigmoid(inX):   return 1.0/(1+exp(-inX))   '''标准bp算法每次更新都只针对单个样例,参数更新得很频繁sdataSet 训练数据集labels 训练 ...

  9. java学习——内部类(二)

    使用内部类 内部类一共有三种使用方法: 1.在外部类中使用内部类 在外部类中使用内部类,与平常使用内部类没有区别,可以直接通过内部类的类名来定义变量,通过new调用内部类的构造方法来创建实例. 唯一的 ...

  10. 吴裕雄--天生自然ShellX学习笔记:Shell 基本运算符

    Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 ...