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 ...
随机推荐
- NumPy 基础用法
NumPy 是高性能科学计算和数据分析的基础包. 它是 pandas 等其他各种工具的基础. 主要功能: ndarray 一个多维数组结构, 高效且节省空间 无需循环对整组数据进行快速运算的数学函数 ...
- (思维导图搞定)Content-Type:application/json,后台如何接收
自己定的规范:只要Content-Type设置为application/json的时候,前台的data要传递字符串 虽然设置为application/json,前台传对象request.getPara ...
- grep init 与 grep [i]nit
看grep的知识点的时候,在XXX博客里看到一个这样的例子,一直在纠结,纠结,init与[i]nit 匹配到的东西不应该时一样的嘛,为什么一个匹配得出来,一个不行.后来在群里问了某位大哥,耐心的讲解, ...
- python3 访问百度返回压缩格式
import urllib, urllib.request, urllib.parse import random import zlib import re import os, time Save ...
- mac os high sierra下搭建php多版本-php5.2+php5.6-nginx
xampp的apache彻底启动不来了. php52的编译参数 ./configure --prefix=/usr/local/Cellar/php52bysk/ --with-config-file ...
- oracle中如何修改用户名和密码
1.以Windows操作系统为例,打开命令提示符,输入命令sqlplus /nolog ,进入oracle控制台,并输入 conn /as sysdba;以DBA角色进入. 2.连接成功后,输入“se ...
- SpringBoot Web开发(3) WebMvcConfigurerAdapter过期替代方案
springboot2.0中 WebMvcConfigurerAdapter过期替代方案 最近在学习尚硅谷的<springboot核心技术篇>,项目中用到SpringMVC的自动配置和扩展 ...
- 模拟SQL用户 EXECUTE AS USER
EXECUTE AS USER= @domain SELECT SUSER_NAME(), USER_NAME(); REVERT 以下语句可以模拟SQL用户,具体使用场景自行脑补.
- 一个简单的通讯服务框架(大家发表意见一起研究)JAVA版本
最近研究下java语言,根据一般使用的情况,写了个连接通讯服务的框架: 框架结构 C-Manager-S; 把所有通讯内容抽取成三个方法接口:GetData,SetData,带返还的Get; 所有数据 ...
- java中随机二维数组中寻找最大值并输出坐标
public static void main(String[] args) { int x=0; int y=0; int a[][]=new int[5][5]; int max=-Integer ...