Leetcode: Construct Binary Tree from Preorder and Inorder Transversal
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的更多相关文章
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
		
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
 - [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 ...
 - 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 ...
 - [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python
		
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...
 - [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 ...
 - 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 ...
 - 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 ...
 - 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 ...
 - 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 ...
 
随机推荐
- Android Log缓冲区大小设置
			
当手机没有连接PC时,手机log缓冲区仍然会保存指定大小的最新log,连接pc,通过adb logcat 仍然可以拿出来 如何查看log缓缓区的大小? 通过adb logcat -g 可以查看 C:\ ...
 - 应用程序创建自己的奔溃转储(crash dump)文件
			
1.注册自定义的UnhandledExceptionFilter,C/C++ Runtime Library下需要注意自定义handler被移除(hook kernel32.dll的SetUnhand ...
 - tornado web开发
			
tornado是python的web框架,这里简单记录下利用tornado怎么实现文件的上传,其中web.py上传功能类似. 直接用代码说明: 代码来自:http://my.oschina.net ...
 - 游戏AI-行为树
			
参考: 游戏AI—行为树研究及实现 GAD腾讯游戏开发者平台:游戏中的人工智能AI 腾讯开源项目behaviac 占坑,待编辑
 - 慕课学习--DNS的作用
			
因为相对于32位的IP地址,人对域名更加敏感,也更容易记忆.所以一般都是把IP地址转化为域名进行网页的访问. DNS(Domain Name System,域名系统),万维网上作为域名和IP地址相互映 ...
 - centos6.9环境下JDK安装部署
			
1.准备jdk安装文件: 这里我使用的是 jdk-7u79-linux-x64.tar.gz 2.在 /usr/local 目录下创建 sotfware目录,并上传JDK文件: 解压文件并修改文件夹为 ...
 - thinkphp与php共享session
			
在其他php页面添加如下代码即可 if (!session_id()) session_start(); 使用时 thinphp 使用 session('test','123'); $user_inf ...
 - IIS7.5 配置虚拟目录的经历
			
好多网站为了不带上什么端口号所有就建立虚拟目录的试来使用80端口 iis6设置都没有问题可到了7.5碰到了点问题 原来7.5上有个:添加应用程序和添加虚拟目录.还有个转换为应用程序 直接把网站建成虚拟 ...
 - linux:帮助命令help、man、info
			
笔记内容如下: 1.内建命令与外部命令之分2.help , man , info命令的使用以及区别 内建命令与外部命令 有一些查看帮助的工具在内建命令与外建命令上是有区别对待的. 内建命令实际上是 s ...
 - 43(function pointer 1)
			
#include<iostream> using namespace std; typedef int A; typedef void (*PF)(); typedef int (*P_A ...