Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
2024-09-03 22:01:50
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;
}
最新文章
- js 获取时间间隔
- Mysql创建新用户后无法登录,提示 Access denied for user &#39;username&#39;@&#39;localhost&#39; (using password: YES)
- selenium 3 对我们的影响
- nginx 的启动脚本
- ubuntu 64位android项目报错的解决方案,打开64位 Ubuntu 的32位支持功能
- CentOS中配置LNMP环境打开提示File not found
- mysql存储过程中传decimal值会自动四舍五入,没有小数
- php-fpm 启动不了 libiconv.so.2找不到
- Nhibernate中 Many-To-One 中lazy=";proxy"; 延迟不起作用的原因
- cf D. Alternating Current
- Linux安全审计命令
- Haproxy的配置
- JAVA多线程---wait() &; join()
- Harris Corner
- 使用Spark MLlib进行情感分析
- .Net Core小技巧 - Swagger适配虚拟目录及二级目录
- CS萌新的汇编学习之路02 Learning of Assembly Language
- python中numpy.ndarray.shape的用法
- Stack Sorting CodeForces - 911E (思维+单调栈思想)
- Web开发——HTML基础(文件和网站结构)
热门文章
- Using Blocks in iOS 4: Designing with Blocks
- Beginning Auto Layout Tutorial in iOS 7: Part 3
- struts2拦截器实现session超时返回登录页面(iframe下跳转到其父页面)
- mac为photoshop添加字体
- *** Python版一键安装脚本
- 解决Linux下AES解密失败
- JAVA启动参数整理
- HttpClient 模拟登录搜狐微博
- SVN客户端忽略无关文件
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)