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;
}

最新文章

  1. js 获取时间间隔
  2. Mysql创建新用户后无法登录,提示 Access denied for user &#39;username&#39;@&#39;localhost&#39; (using password: YES)
  3. selenium 3 对我们的影响
  4. nginx 的启动脚本
  5. ubuntu 64位android项目报错的解决方案,打开64位 Ubuntu 的32位支持功能
  6. CentOS中配置LNMP环境打开提示File not found
  7. mysql存储过程中传decimal值会自动四舍五入,没有小数
  8. php-fpm 启动不了 libiconv.so.2找不到
  9. Nhibernate中 Many-To-One 中lazy=&quot;proxy&quot; 延迟不起作用的原因
  10. cf D. Alternating Current
  11. Linux安全审计命令
  12. Haproxy的配置
  13. JAVA多线程---wait() &amp; join()
  14. Harris Corner
  15. 使用Spark MLlib进行情感分析
  16. .Net Core小技巧 - Swagger适配虚拟目录及二级目录
  17. CS萌新的汇编学习之路02 Learning of Assembly Language
  18. python中numpy.ndarray.shape的用法
  19. Stack Sorting CodeForces - 911E (思维+单调栈思想)
  20. Web开发——HTML基础(文件和网站结构)

热门文章

  1. Using Blocks in iOS 4: Designing with Blocks
  2. Beginning Auto Layout Tutorial in iOS 7: Part 3
  3. struts2拦截器实现session超时返回登录页面(iframe下跳转到其父页面)
  4. mac为photoshop添加字体
  5. *** Python版一键安装脚本
  6. 解决Linux下AES解密失败
  7. JAVA启动参数整理
  8. HttpClient 模拟登录搜狐微博
  9. SVN客户端忽略无关文件
  10. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)