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. mac 配置sencha touch环境

    1 安装 java 2 安装 node js 为使用npm作准备 3 用npm命令安装 cordova npm install -g cordova

  2. 【温故知新】——Bootstrap响应式知识点复习

    前言:本文是自己在学习课程中的课程笔记,这里用来温故知新的,并非本人原创. 开发工具 1.记事本,Editplus,... ... 2.Sublime,Dreamweaver 3.Webstorm = ...

  3. 内网ip打洞-----p2p实现原理

    网上找了非常多.代码大堆,原理讲清楚透彻的不多. 本人找几篇讲得好的来整理一下. 一片技术文章,最基本的讲清楚原理.假设再有完整的能执行的源码也可,关键是要把核心部分代码分析清楚. (1)问题的由来: ...

  4. tyvj-1460 旅行

    题目描写叙述: A国有n座城市,每座城市都十分美,这使得A国的民众们很喜欢旅行. 然而,A国的交通十分落后,这里仅仅有m条双向的道路.而且这些道路都十分崎岖,有的甚至还是山路.仅仅能靠步行.通过每条道 ...

  5. linux:date命令(转)

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  6. swiper-demo1

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. StringUtils方法

    org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的( ...

  8. POJ 2985 Treap平衡树(求第k大的元素)

    这题也能够用树状数组做,并且树状数组姿势更加优美.代码更加少,只是这个Treap树就是求第K大元素的专家--所以速度比較快. 这个也是从那本红书上拿的模板--自己找了资料百度了好久,才理解这个Trea ...

  9. Highcharts使用表格数据绘制图表

    Highcharts使用表格数据绘制图表 在Highcharts中,同意用户使用网页中现有的表格数据作为数据来源,然后依据该数据来源绘制图表.对于一个典型的HTML表格.当中,第一列的数据会作为x轴刻 ...

  10. Ubuntu下安装和编译ffmpeg

    参考:http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu   1.安装依赖包 sudo apt-get update sudo apt-get -y ...