[抄题]:

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

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

For example, given

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

Return the following binary tree:

    3
/ \
9 20
/ \
15 7

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

距离太远就要相加。相同的题还是一起做比较好,隔一段时间再去理解 实在太心累了。

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

[一句话思路]:

int idx = map.get(posorder[posStart]); 从postorder中取出index作为后续使用才行

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

[画图]:

[一刷]:

  1. 比较远时,加上中-右 = inidx - inend

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

都怪recursive不好跑case,算了,距离太远就要相加。

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

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

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

[代码风格] :

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

[潜台词] :

class Solution {

    public TreeNode buildTree(int[] inorder, int[] posorder) {
//corner case
if (inorder == null || posorder == null || posorder.length != inorder.length) return null; //initialization: put (posorder[i], i) into map
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < inorder.length; i++)
map.put(inorder[i] , i); //dfs and return
return dfs(inorder, 0, inorder.length - 1, posorder, posorder.length - 1, 0, map);
} public TreeNode dfs(int[] inorder, int inStart, int inEnd,
int[] posorder, int posStart, int posEnd,
HashMap<Integer, Integer> map) {
//exit case
if (inStart > inEnd || posStart > posEnd) return null; //find inIdx and do dfs
TreeNode root = new TreeNode(posorder[posStart]);
int inIdx = map.get(root.val); //do dfs in left and right and add to root
root.left = dfs(inorder, inStart, inIdx - 1, posorder, posStart + (inIdx - inEnd) - 1, posEnd, map);
root.right = dfs(inorder, inIdx + 1, inEnd, posorder, posStart- 1, posEnd, map); return root;
}
}

106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树的更多相关文章

  1. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

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

  2. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

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

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

  4. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  5. LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

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

  6. 106. Construct Binary Tree from Inorder and Postorder Traversal

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

  7. C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

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

  8. 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  9. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

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

随机推荐

  1. mongodb与mysql命令详细对比

    传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(docu ...

  2. 黄聪:谷歌验证 (Google Authenticator) 的实现原理是什么?

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:徐小花链接:http://www.zhihu.com/question/20462696/answer/18731073来源: ...

  3. git与github区别与简介

    From: https://blog.csdn.net/skyxmstar/article/details/65631658 git和github是两个完全不同的概念. git 是一个版本管理工具,是 ...

  4. Android Gradle 依赖方式

    Android Gradle 依赖方式有以下6种: Compile compile是对所有的build type以及favlors都会参与编译并且打包到最终的apk文件中. Provided Prov ...

  5. centos7如何查看网络状态?

    参考https://www.jb51.net/os/RedHat/520187.html 查看网络状态: lsof -Pnl +M -i4 显示ipv4服务及监听端情况 netstat -anp 所有 ...

  6. 基于keras的fasttext短文本分类

    ### train_model.py ### #!/usr/bin/env python # coding=utf-8 import codecs import simplejson as json ...

  7. Python【每日一问】01

    问:深拷贝.浅拷贝.直接赋值的区别是什么?并举例说明 1.区别 (1)直接赋值:对象的引用 (2)浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象 (3)深拷贝(deepcopy): cop ...

  8. 1、根"/"目录结构

    1.目录结构 FSH [root@localhost /]# tree -L . ├── bin -> usr/bin #普通用户使用的命令 ├── boot #存放系统启动相关文件,例如ker ...

  9. delphi combobox屏蔽鼠标滑动

    //第1种方法 procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; Mo ...

  10. ARM920T的Cache

    转载自:http://www.eefocus.com/mcu-dsp/242034 ARM920T有16K的数据Cache和16K的指令Cache,这两个Cache是基本相同的,数据Cache多了一些 ...