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 ...
随机推荐
- python 连接 oracle 统计指定表格所有字段的缺失值数
python连接oracle -- qlalchemy import cx_Oracle as co import pandas as pd from sqlalchemy import crea ...
- Object.is()
- 阿里云centos5升级yum源为6
升级后出现Errno -3] Error performing checksum 需要安装 python-hashlib Python 2.4 安装 hashlib 2012年11月13日 14:29 ...
- [zz]如何学习Polygon Mesh Processing这本书?
图形学初学者,如何学习Polygon Mesh Processing这本书?修改修改 导师暑假让我看看这本书,目前看了一半觉得这本书比较偏重数学基础,对于具体的 implementation提及的并不 ...
- ubuntu server cloud img username password
新安装了OpenStack Queens发现无镜像,蹦蹦跳跳的下载了ubuntu的镜像 网址https://cloud-images.ubuntu.com/ 最好你自己找你想要的,vmdk.ova.i ...
- solr字段压缩属性compressed新版本已经移除
solr字段压缩属性compressed新版本已经移除 可能是考虑到压缩意义不大还减少搜索效率,所以去掉了.而且好像没有替代属性.
- C#使用ITextSharp操作pdf
在.NET中没有很好操作pdf的类库,如果你需要对pdf进行编辑,加密,模板打印等等都可以选择使用ITextSharp来实现. 第一步:可以点击这里下载,新版本的插件升级和之前对比主要做了这几项重大改 ...
- 性能测试day06_需求设计的学习(性能重中之重,思维方向永远重于工具)
今天接着来学习下性能知识,本来是应该先学习一下LR的用法的,不过听完云层大大的课之后,我感觉工具没有这一篇来的重要,我们知道性能有三大步骤:负载->监控->调优,但是在这个之前我们首先要搞 ...
- 24种java设计模式总结和目录
https://blog.csdn.net/qq_40369829/article/details/80374131 简介原则分类创建型模式结构型模式行为型模式类图参考简介设计模式是在特定环境下,为解 ...
- Linux下安装GitHub
安装GitHub 1.下载安装git: yum -y install git git-core git-doc 2.运行完执行 ssh-keygen -t rsa -C "your@emai ...