105. Construct Binary Tree from Inorder and preorder Traversal
/*
* 105. Construct Binary Tree from Inorder and preorder Traversal
* 11.20 By Mingyang
* 千万不要以为root一定在中间那个点,还是要找一下中间点的位置
* p.left = construct(preorder, preStart + 1, preStart + (k - inStart),inorder, inStart, k - 1);
* p.right = construct(preorder, preStart + (k - inStart) + 1, preEnd,inorder, k + 1, inEnd);
* 难点就在于如何找到这两个起点和终点的index
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
int preEnd = preorder.length - 1;
int inEnd = inorder.length - 1;
return construct(preorder, 0, preEnd, inorder, 0, inEnd);
}
public TreeNode construct(int[] preorder, int preStart, int preEnd,int[] inorder, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) {
return null;
}
int val = preorder[preStart];
TreeNode p = new TreeNode(val);
// find parent element index from inorder
int k = 0;
for (int i = 0; i < inorder.length; i++) {
if (val == inorder[i]) {
k = i;
break;
}
}
p.left = construct(preorder, preStart + 1, preStart + (k - inStart),inorder, inStart, k - 1);
p.right = construct(preorder, preStart + (k - inStart) + 1, preEnd,inorder, k + 1, inEnd);
return p;
}
105. Construct Binary Tree from Inorder and preorder Traversal的更多相关文章
- leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- android 代码中及xml中设置透明
在布局文件的属性中,比如要设置一个LineaerLayout的背景为灰色透明.首先查RGB颜色表灰色是:#9E9E9E,AA代表透明,(透明度从00到FF,00表示完全透明),所以,设置其属性:and ...
- (转)搭建Spring4.x.x开发环境
http://blog.csdn.net/yerenyuan_pku/article/details/52831306 先去Spring官网下载Spring4.x.x开发包(本人使用的版本是Sprin ...
- swfit:运算符重载 Operator Methods
Operator Methods Classes and structures can provide their own implementations of existing operators. ...
- Dart开发环境搭建
一.SDK的安装与环境配置 1. 下载Dark SDK http://www.gekorm.com/dart-windows/ 2. 安装SDK 3. 配置环境变量(一般已经默认生成好了,这里可以 ...
- C++ new delete(一)
在C#.Java這種managed語言,因為有garbage collection,所以完全不用考慮free()或delete,但在C/C++,有時候要delete的,有時又不用,到底哪些改delet ...
- (转)浅谈trie树
浅谈Trie树(字典树) Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问 ...
- UVA12633 Super Rooks on Chessboard
题目描述 题解: 第一眼满眼骚操作,然后全部否掉. 然后屈服于题解,才发现这题这么执掌. 首先,如果这个东西是普通的车,那我们可以记录一下$x,y$的覆盖情况,然后减一下; 但是这个可以斜着走. 所以 ...
- DBLINK引起的SQL性能问题
最近发现报表系统上有一存储过程越来越慢,在数据库中查询后,发现有以下条SQL --优化前:耗时>1h select c.policyno, c.endorseno, r.item_code, s ...
- mysql 删除恢复
一.模拟误删除数据表的恢复 1 二进制日志功能启用 vim /etc/my.cnf [mysqld] log-bin 2 完全备份 mysqldump -A -F --master-data=2 - ...
- mysql多字段组合删除重复行
DELETEFROM boll_paramWHERE id in ( SELECT a.id FROM ( SELECT id FROM boll_param WHERE (symbol, time_ ...