[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

if (preStart > preEnd || inStart > inEnd) 退出条件是>,因为等于的时候还可以继续新建节点

[思维问题]:

不知道怎么dc:参数就是数组的index就行了,分成start1 end1  start2 end2,多开几个变量就行了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

通过hashmap记录下pre的中节点在in中的位置,然后左右dc

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. HashMap<Integer, Integer> map 函数中已经建立好了的map不需要加括号,新建map才需要

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

不知道怎么dc:参数就是数组的index就行了,分成start1 end1  start2 end2,多开几个变量就行了

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* 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) {
//corner case
if (preorder == null && inorder == null) return null; //initialization : put all the inorder into map
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < inorder.length; i++)
map.put(inorder[i], i); //return
return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, map);
} public TreeNode buildTreeHelper(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, HashMap<Integer, Integer> map) {
//exit if the bounds exceeds
if (preStart > preEnd || inStart > inEnd) return null; //build a new root
TreeNode root = new TreeNode(preorder[preStart]);
int inIdx = map.get(root.val);
int numsLeft = inIdx - inStart; //divid and conquer to form left and right
root.left = buildTreeHelper(preorder, preStart + 1, preEnd, inorder, inStart, inIdx - 1, map);
root.right = buildTreeHelper(preorder, preStart + numsLeft + 1, preEnd, inorder, inIdx + 1, inEnd, map); return root;
}
}

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

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

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

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

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

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

  8. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

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

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

随机推荐

  1. MySQL Event--Event and EventScheduler

    MySQL Event 创建EVENT语法: CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] event_name O ...

  2. .htaccess FollowSymlinks影响rewrite功能

    Thinkphp的框架的根目录的.htaccess是这样写的: <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine ...

  3. Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found

    Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found 网上大部分文章都是说在bios开启vt-x支持等.这里 ...

  4. 使用sshpass方式实现ssh自动登录

    1:sshpass下载地址(用yum安装不了)       https://sourceforge.net/projects/sshpass/files/ or wget http://sourcef ...

  5. [C#]typeof,Gettype()和is的区别

    typeof 参数是一个类型名称,比如你自己编写的一个类 GetType()是类的方法,继承自object,返回该实例的类型 is 用来检测实例的兼容性(是否可以相互转换) 例: class Anim ...

  6. windows与linux换行规则

    在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符.但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字符.要是在这 ...

  7. maya中MFnMesh.h使用说明的翻译

    由于最近要修改一个maya中的deformer脚本,于是开始系统学习openMaya的一些知识,当然少不了得把一堆头文件说明看一遍.首先把MFnMesh.h这个文件翻译一下吧,不废话,上译文: 首先M ...

  8. 1T硬盘获3T体验 彻底解决NVR存储时间短的问题

    随着高清技术的进步,现在300W和400W的IPC越来越普及,但同时带来了更多的成本及存储便利问题.“硬盘存了7天就满了”.“同样大小的硬盘,存储时间越来越短”......为啥你的NVR不能存更长的时 ...

  9. IntelliJ IDEA中Terminal路径的问题(win7环境)

    在安装java jdk,配置系统变量后,再安装idea,有时候会出现使用idea中Termimal进行编译运行java文件出现,javac/java不是内部命令,或者“错误: 找不到或无法加载主类”的 ...

  10. Let'sencrypt.sh 抛出异常: Response: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)>

    起因 今天网站的SSL证书过期了,打算重新申请,运行 Let'sencrypt.sh 的时候抛出了这么个异常. 一番搜索,发现居然找不到直接的答案.没有直接的答案就只能通过间接的答案来解决了. 希望我 ...