Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

难度:95,参考了网上的思路。这道题是树中比较有难度的题目,需要根据先序遍历和中序遍历来构造出树来。这道题看似毫无头绪,其实梳理一下还是有章可循的。下面我们就用一个例子来解释如何构造出树。
假设树的先序遍历是12453687,中序遍历是42516837。这里最重要的一点就是先序遍历可以提供根的所在,而根据中序遍历的性质知道根的所在就可以将序列分为左右子树。比如上述例子,我们知道1是根,所以根据中序遍历的结果425是左子树,而6837就是右子树。接下来根据切出来的左右子树的长度又可以在先序便利中确定左右子树对应的子序列(先序遍历也是先左子树后右子树)。根据这个流程,左子树的先序遍历和中序遍历分别是245和425,右子树的先序遍历和中序遍历则是3687和6837,我们重复以上方法,可以继续找到根和左右子树,直到剩下一个元素。可以看出这是一个比较明显的递归过程,对于寻找根所对应的下标,我们可以先建立一个HashMap,以免后面需要进行线行搜索,这样每次递归中就只需要常量操作就可以完成对根的确定和左右子树的分割。
算法最终相当于一次树的遍历,每个结点只会被访问一次,所以时间复杂度是O(n)。而空间我们需要建立一个map来存储元素到下标的映射,所以是O(n)。

这道题的难点在于怎么用一个HashMap来建立preorder和inorder的关系。inorder和preorder两个数组里面,对应点的值应该是一样的。利用这一点,再加上我们一开始知道的是preorder的根的位置和值,自然而然的就会想到利用这个根的值去寻找它在inorder里面的位置。所以自然HashMap的key应该是inorder数组值,value是index

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0 || inorder.length == 0 || preorder.length != inorder.length) return null;
int len = preorder.length;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer> ();
for (int i=0; i<len; i++) {
map.put(inorder[i], i);
}
return helper(preorder, 0, len-1, inorder, 0, len-1, map);
} public TreeNode helper(int[] preorder, int preL, int preR, int[] inorder, int inL, int inR, HashMap<Integer, Integer> map) {
if (preL > preR || inL > inR) {
return null;
}
TreeNode root = new TreeNode(preorder[preL]);
int index = map.get(preorder[preL]);
root.left = helper(preorder, preL+1, preL+index-inL, inorder, inL, index-1, map);
root.right = helper(preorder, preL+index-inL+1, preR, inorder, index+1, inR, map);
return root;
}
}

Leetcode: Construct Binary Tree from Preorder and Inorder Transversal的更多相关文章

  1. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. [LeetCode] 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 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 ...

  4. [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...

  5. [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:  You may assume tha ...

  6. LeetCode——Construct Binary Tree from Preorder and Inorder Traversal

    Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...

  7. LeetCode -- Construct Binary Tree from Preorder and Inorder

    Question: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may as ...

  8. Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal

    总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...

  9. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

随机推荐

  1. centos 7安装jdk、tomcat

    jdk安装 创建上传目录: [root@ckl1 home]# pwd /home [root@ckl1 home]# mkdir upload 安装上传工具: yum install lrzsz 上 ...

  2. thinkphp 构建子查询

      thinkphp构建子查询sql语句写法        从3.0版本开始新增了子查询支持,有两种使用方式: 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是 ...

  3. 改变presentModalView大小

    rc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; rc.modalPresentationStyle = UIModalP ...

  4. -bash: locate: command not found

    部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum  -y ...

  5. shell脚本中对简单实现对log的处理

    用shell在写小程序时,log没用像python样用logging模块可以直接使用,下面我们就简单写下用shell函数来实现log分级 #/bin/bash sys_log="/var/l ...

  6. Java虚拟机五 堆的参数配置

    堆空间是Java进程的重要组成部分,几乎所有的应用相关的内存空间都和堆有关. 1.最大堆和初始堆的设置 当Java程序启动时,虚拟机就会分配一块初始堆空间,使用参数 -Xms 指定这块空间的大小.一般 ...

  7. 什么是webpack?

    https://www.webpackjs.com/concepts/ https://webpack.github.io/ 本质上,webpack 是一个现代 JavaScript 应用程序的静态模 ...

  8. c++的矩阵乘法加速trick

    最近读RNNLM的源代码,发现其实现矩阵乘法时使用了一个trick,这里描述一下这个trick. 首先是正常版的矩阵乘法(其实是矩阵乘向量) void matrixXvector(float* des ...

  9. 关于python爬虫的编码错误

    现在才发现很多错误只有自己一点点的去尝试才能发现.不管之前别人怎么和你说,总是不可能面面俱到,所以提升自己的方法就是亲手实践,自己一点点的去发现问题,并一个个的解决.慢慢享受其中无言的快感. 今天就发 ...

  10. POJ_3264_Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 57259   Accepted: 26831 ...