[抄题]:

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. ios-上传图片到后台

    做第一个项目时,有个版块的个人信息的编辑涉及到头像修改,老大说项目里有通用的代码,让我自己去找.总算找到,搞了许久才弄好,看来理解能力还需要提高啊!! #pragma mark- 修改头像上传后保存 ...

  2. 解决Visual Studio禁止使用strlen函数的问题

    问题描述: 在学习C++的复制构造函数以及复制赋值运算符的重载时,需要用到使用C风格的字符串作为引入,由于我用的是VS2015(社区版),在编译时出错.编译器提醒strcpy函数是不安全的,建议改用s ...

  3. OpenResty 最佳实践 lua与nginx的结合 --引用自https://moonbingbing.gitbooks.io/openresty-best-practices/content/

    系统的说明了lua在nginx上的开发 请大家到源址查看 OpenResty最佳实践

  4. Delphi调用大漠插件示例

    Delphi XE2 版本调用大漠插件方法:打开Component->Import Component->默认Import a Type Library,点击Next->找到Dm.d ...

  5. MySQL数据库中统计一个库中的所有表的行数?

    今天公司两个远端的数据库主从同步有点问题,查看下wordpress库下所有表的表的条目? mysql> use information_schema;Database changedmysql& ...

  6. 关于Java流

  7. Linux查看端口占用情况并释放端口占用

    1.netstat -tunlp:查看所有tcp/udp端口占用及进程相关信息 2.netstat -tln | grep 端口号:查看特定端口占用情况 3.kill -9 进程ID(PID):释放指 ...

  8. Java笔试面试题整理第六波(修正版)

    转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  9. clear 属性

    clear属性:规定元素的哪一侧不允许有其他的浮动元素 Example: <html> <head> <style type="text/css"&g ...

  10. 3.Jmeter参数化

    1.参数化的三种方式 ${变量} 1 用户定义的变量 2 添加配置元件 CSV Data Set Config ,导入.csv文档 3 利用函数助手中的函数获取参数值     主要用 _Random函 ...