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. IOS7开发~错误收集

    1. fatal error: file '/Applications/Xcode5-DP.app/Contents/Developer/Platforms/iPhoneSimulator.platf ...

  2. EasyMvc入门教程-基本控件说明(7)文字块导航

    文字块导航其实就是开发winform时候常见的 带Title的Group面板..~!@#¥..好吧,没开发过winform的同学看下图: 实现代码如下: @Html.Q().BlockField(). ...

  3. 通过UltraISO来提取U盘启动盘的ISO镜像文件

    我们先来说下UltraISO这个工具,中文名也叫软碟通,他是一个无需量产你的U盘就可以把U盘做成启动盘的工具,当然了,这么强大的工具肯定不是免费版的,对,他是共享的:但是你可以下载特别版嘛..网上到处 ...

  4. linux中nl用法

    linux 中nl 命令使用 nl :添加行号打印 -b:   指定行号指定的方式,主要有两种:    -b a : 表示不论是否为空行,都同样列出行号    -b t : 如果有空行,则不列出那一行 ...

  5. Web开发人员不容错过的10个HTML5工具

    HTML5已经成为当今世界的一个必定组成部分.由于World Wide Web万维网是使用超文本标记语言来架构和呈现的,于是HTML5成为了最流行的编程语言之中的一个.随着网络的不断扩张,Web开发者 ...

  6. java web邮件收发

    1.网上方法要导入两个包 mail.jar&activation.jar package com.zjh.shopping.util; import java.util.Date; impor ...

  7. cart算法

  8. 以前整理的网络上免费API接口

    以前整理的一些免费的API接口,具体是否好用还需要时间测试,但是先分享给大家. 天气接口 聚合数据: http://op.juhe.cn/onebox/weather/query 用例 官方文档 来源 ...

  9. Android开发之WebView具体解释

    概述: 一个显示网页的视图.这个类是你能够滚动自己的Web浏览器或在你的Activity中简单地显示一些在线内容的基础.它使用了WebKit渲染引擎来显示网页,包含向前和向后导航的方法(通过历史记录) ...

  10. UNP学习笔记(第十六章 非阻塞I/O)

    套接字的默认状态时阻塞的 可能阻塞的套接字调用可分为以下4类: 1.输入操作,包括read.readv.recv.recvfrom和recvmsg. 2.输入操作,包括write.writev.sen ...