Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路一:
preorder[0]为root,以此分别划分出inorderLeft、preorderLeft、inorderRight、preorderRight四个数组,然后root.left=buildTree(preorderLeft,inorderLeft); root.right=buildTree(preorderRight,inorderRight)
JAVA实现如下:
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0 || preorder.length != inorder.length)
return null;
TreeNode root = new TreeNode(preorder[0]);
int index = 0;
for (int i = 0; i < inorder.length; i++)
if (inorder[i] == root.val) {
index = 0;
break;
}
int[] inorderLeft = new int[index], preorderLeft = new int[index];
int[] inorderRight = new int[inorder.length - 1 - index], preorderRight = new int[inorder.length
- 1 - index];
Set<Integer> inorderLeftSet=new HashSet<Integer>();
for (int i = 0; i < inorderLeft.length; i++){
inorderLeft[i] = inorder[i];
inorderLeftSet.add(inorder[i]);
}
for (int i = 0; i < inorderRight.length; i++)
inorderRight[i] = inorder[index + i + 1]; int j = 0, k = 0;
for (int i = 0; i < preorder.length; i++) {
if(inorderLeftSet.contains(preorder[i]))
preorderLeft[j++]=preorder[i];
else if(preorder[i]!=root.val)
preorderRight[k++]=preorder[i];
}
if(buildTree(preorderLeft,inorderLeft)!=null)
root.left=buildTree(preorderLeft,inorderLeft);
if(buildTree(preorderRight,inorderRight)!=null)
root.right=buildTree(preorderRight,inorderRight);
return root;
}
结果:Time Limit Exceeded
解题思路二:出现上次解法的原因是因为本人把前序遍历理解成了层次遍历,其实本题是《编程之美》3.9节 重建二叉树的原题,书中已经给出的答案,JAVA实现如下:
static public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTree(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
}
static public TreeNode buildTree(int[] preorder, int[] inorder, int pBegin, int pEnd, int iBegin, int iEnd){
if(pBegin>pEnd)
return null;
TreeNode root = new TreeNode(preorder[pBegin]);
int i = iBegin;
for(;i<iEnd;i++)
if(inorder[i]==root.val)
break;
int lenLeft = i-iBegin;
root.left = buildTree(preorder, inorder, pBegin+1, pBegin+lenLeft, iBegin, i-1);
root.right = buildTree(preorder, inorder, pBegin+lenLeft+1, pEnd, i+1, iEnd);
return root;
}
Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
随机推荐
- mysql日常运维与参数调优
日常运维 DBA运维工作 日常 导数据,数据修改,表结构变更 加权限,问题处理 其它 数据库选型部署,设计,监控,备份,优化等 日常运维工作: 导数据及注意事项 数据修改及注意事项 表结构变更及注意事 ...
- xamarin.android 资源图片问题
在xamarin.android 中,关于图片的资源一般都在Resources.drawable下面,在Resources这个文件夹下面,包含了drawable.drawale-hdpi.drawab ...
- 在html里网页中嵌入优酷的视频
<html> <embed src="http://player.youku.com/player.php/sid/XMjAzOTk4NjI4/v.swf" qu ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...
- thinkphp5 中使用postgresql 缺少函数 table_msg
参考解决办法 在postgresql 中执行命令 插入函数 https://blog.csdn.net/sinat_35767703/article/details/76070236?utm_sour ...
- sakila演示数据库安装
下载地址:https://dev.mysql.com/doc/index-other.html 安装帮助文档:https://dev.mysql.com/doc/sakila/en/sakila-in ...
- 让你十分钟学会shell
1.先介绍下shell的工作原理 Shell可以被称作是脚本语言,因为它本身是不需要编译的,而是通过解释器解释之后再编译执行,和传统语言相比多了解释的过程所以效率会略差于传统的直接编译的语言. 这是s ...
- 在EA中用ER图生成数据库
ER图 E-R图也称实体-联系图(Entity Relationship Diagram).提供了表示实体类型.属性和联系的方法.用来描写叙述现实世界的概念模型. 实体就是看的见摸得着或者能被人感知接 ...
- SQL存在一个表而不在还有一个表中的数据
select a.id,a.oacode,a.custid,a.custname,a.xsz,a.salename,a.communicationtheme,a.communicationproper ...
- C语言内存分配函数malloc——————【Badboy】
C语言中经常使用的内存分配函数有malloc.calloc和realloc等三个,当中.最经常使用的肯定是malloc,这里简单说一下这三者的差别和联系. 1.声明 这三个函数都在stdlib.h库文 ...