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的更多相关文章

  1. [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 ...

  2. (二叉树 递归) 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

  7. leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树

    不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...

  8. [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...

  9. 【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 ...

随机推荐

  1. Android Retrofit RxJava实现缓存

    RxJava如何与Retrofit结合参考:http://blog.csdn.net/jdsjlzx/article/details/52015347 缓存配置 app网络数据的离线缓存实现有很多种办 ...

  2. W​i​n​d​o​w​s​2​0​0​3​建​立​F​T​P​服​务​器以及报530 User <用户名> cannot log in home directory inaccessible的解决方法

    W​i​n​d​o​w​s​2​0​0​3​建​立​F​T​P​服​务​器:                   W​i​n​d​o​w​s​2​0​0​3​建​立​F​T​P​服​务​器 FTP连接 ...

  3. ClassPathXmlApplicationContext和FileSystemXmlApplicationContext区别

    ClassPathXmlApplicationContext 默认文件路径是src下那一级classpath:和classpath*:的区别: classpath: 只能加载一个配置文件,如果配置了多 ...

  4. MySQL主库异常,从库手动切换为主库方案

    主库异常,从库手动切换为主库方案 1.登录从服务器,确认从服务器已经完成所有同步操作: mysql> stop slave io_thread mysql> show processlis ...

  5. centos7 下编译ffmpeg

    下载包: wget https://ffmpeg.org/releases/ffmpeg-4.1.tar.gz GCC如果没有yasm: http://yasm.tortall.net/Downloa ...

  6. 2016.11.14 MIT challenge之课程总览

    Degree Chartshttp://catalog.mit.edu/degree-charts/computer-science-engineering-course-6-3/ MIT Chall ...

  7. Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)

    导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...

  8. eclipse高速查找一个变量、方法或者类被引用的地方

    近期不停debug,拿到一个变量之后总是要先概览一下才好下手,之前一直用Ctrl+F来做,太麻烦. 今天查了下eclipse使用,发现有快捷键,用法: 先双击要查看的变量.方法或者类,使之被选中,然后 ...

  9. Html5 meta 笔记

    摘抄:原文地址:http://www.kmapk.com/html/help/02/127.html 一.天猫 <title>天猫触屏版</title> <meta co ...

  10. shell脚本学习笔记 (流编辑器sed)

    sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用很普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. sed不 ...