[抄题]:

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. alert大法看执行流程(一次采坑)

    页面的dom元素加载完了,给元素一次性添加事件. 收获:事件都是一次性给添加好的,不是点击一次,................................................... ...

  2. Laravel 5.5处理 Emoji 表情不顯示問題

    服务器环境:PHP7 + MySQL5.6 + Laravel 5.5 項目有個玩樂日誌功能,添加玩樂日誌有富文本輸入,富文本輸入的內容在emoji表情之後被截斷了,沒保存到數據表,排查是對應字段字符 ...

  3. 1.1.8 怎样在Word的页眉中插入一级标题

    可以通过域来实现,其具体的操作步骤: 1.为章.节标题使用标题样式.例如:章标题使用标题1样式,节标题使用标题2样式.操作方法:选中章(节)标题,然后点击选项卡中“样式”中的). 2.设置文档页眉和页 ...

  4. vue组件之间数据的传递

    父子组件通信 父组件向子组件传递数据: 1.通过子组件的 props 选项声明它期待获得的数据,用以接收父组件传过来的值. 2.在子组件标签中使用子组件props中创建的属性 3.父组件中注册子组件 ...

  5. 为嵌入式mplayer移植添加ALSA音频驱动(全志V3s荔枝派zero)

    首先准备mplayer和alsa_lib,我的是bulidroot添加后编译自动下载的,版本分别是alsa-lib-1.1.4.1和mplayer-1.3.0. 首先编译alsa_lib: ./con ...

  6. 解决Ubuntn安装中文语言包却不能切换

    记一次奇葩的经历吧,第一次在VM中安装Ubuntn16.04安装完成后的确出现了语言包安装提示,就这样毫无压力的一直用着中文版的Ubuntn. 习惯了一段时间后第二次安装一样的安装方式却始终是英文界面 ...

  7. JAVA web端JS下载excel文件

    JSP代码如下: JSP端引入jquery.easyui.min.js库: <script type="text/javascript" src="<c:ur ...

  8. 几种不同格式的json解析

    原文地址:http://blog.csdn.net/whx405831799/article/details/42171191 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是 ...

  9. PO & SO Integration By IDOC in CNABB

    PO & SO Integration By IDOC in CNABB 话说博主来ABB一个多月时间了,虽然对ABB系统内的流程和配置不是很了解,但对ABB系统内使用的PO和SO通过idoc ...

  10. day34进程相关

    进程1 什么是进程    进程指的是一个正在进行/运行的程序,进程是用来描述程序执行过程的虚拟概念    进程vs程序    程序:一堆代码    进程:程序的执行的过程    进程的概念起源于操作系 ...