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 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作为后续使用才行
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 比较远时,加上中-右 = 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根据后中序数组恢复出原来的树的更多相关文章
- 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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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: ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- (二叉树 递归) 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 ...
随机推荐
- IO流小笔记
File file=new File ();括号里面写路径 exists()判断文件是否存在:isfile()是判断已经存在的文件是文件还是目录: mkdir()和createNewFile()区别在 ...
- goaccess 通过jsonpath 转换为prometheus metrics
goaccess 是一个不错的日志分析工具,包含了json 数据同时支持基于websocket 的实时数据处理,当然我们可以通过jsonpath 的exporter 转换为支持promethues 的 ...
- exec 与文件描述符
参考http://blog.csdn.net/baoendemao/article/details/51638746 1:用法 exec 3<2.txt 以只读方式打开2.txt, ...
- Linux之ls
命令功能: ls是list的简写,列出目录下的内容 命令格式: ls [OPTION]... [FILE]... 命令参数: -a,--all 不忽略以“.”开头的隐藏文件 -A, --almo ...
- centos7.4 64位安装 google-chrome 与 chromedriver 运行 Python selenium 项目
centos7.4 实例 利用 yum 命令安装 google-chrome 超级简单(安装最新版): yum install https://dl.google.com/linux/direct/g ...
- 为嵌入式全志V3s荔枝派板卡添加USB MT7601U(小米随身WIFI)驱动
折腾了了一天终于scan出环境热点了,感觉本来挺简单的事情,网上教程一大把还费了一天的劲,很丧.不过网上教程虽多,但是还还是有些不同之处的,现在特意总结一下 全志V3s荔枝派板卡 添加该驱动的过程. ...
- 红米note3发热严重,小米真垃圾!
红米note3全网通高配版,高通处理器骁龙650(MSM8956),夏天在有空调的房间,上网几分钟手机发烫,真垃圾! ROM已经是官方最新稳定版.MIUI8.5.2.0(LHNCNED) 红米NOTE ...
- [转]MySQL group_concat设置group_concat_max_len
GROUP_CONCAT函数用于将多个字符串连接成一个字符串,在拼接成字符串时就会存在拼接长度的问题,mysql 默认的拼接最大长度为1024 个字节,由于1024个字节会出现不够用的情况,所以有时需 ...
- autofac中文文档
https://autofaccn.readthedocs.io/zh/latest/index.html
- 谷歌浏览器内核Cef js代码整理(二) 滚动条
1.隐藏滚动条 document.documentElement.style.overflow = 'hidden';隐藏竖向滚动条:document.documentElement.style.ov ...