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. PAT Advanced 1135 Is It A Red-Black Tree (30) [红⿊树]

    题目 There is a kind of balanced binary search tree named red-black tree in the data structure. It has ...

  2. Django的URL路由基础

    一.概述 URL路由在Django项目中的体现就是urls.py文件,这个文件可以有很多个,但绝对不会在同一目录下.实际上Django提倡项目有个根urls.py,各app下分别有自己的一个urls. ...

  3. uploadifive使用笔记

    官网地址:http://www.uploadify.com/ uploadifive 是基于H5开发,所以支持移动端和PC端 <input type="file" name= ...

  4. 取石子游戏(gcd)

    蒜头君和花椰妹在玩一个游戏,他们在地上将n颗石子排成一排,编号为1到n.开始时,蒜头君随机取出了2颗石子扔掉,假设蒜头君取出的2颗石子的编号为a, b.游戏规则如下,蒜头君和花椰妹2人轮流取子,每次取 ...

  5. Ubuntu---VIM 常用命令

    今天学习 VIM 的一些常用命令,向传说中的“最后一个编辑器”进攻,哈哈 插入命令: # insert i : 当前光标之前插入 I : 在此行的行首插入 o : 在下一行新起一行插入 O : 在上一 ...

  6. 内存管理-ARC

    推荐文章:http://blog.csdn.net/primer_programer/article/details/14442899 strong:强指针,指向对象,会持有对象,只有当对象无stro ...

  7. keras字符编码

    https://www.jianshu.com/p/258a21ae0390https://blog.csdn.net/apengpengpeng/article/details/80866034#- ...

  8. android weight

  9. 项目课 day01

    ## 电商 ### 定义 指在互联网(Internet).内部网(Intranet)和增值网(VAN,Value Added Network)上以电子交易方式进行交易活动和相关服务活动 ### 电商的 ...

  10. Metasploit详解

    title date tags layout Metasploit 详解 2018-09-25 Metasploit post 一.名词解释 exploit 测试者利用它来攻击一个系统,程序,或服务, ...