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 duplicates do not exist in the tree.
题目标签:Array, Tree
题目给了我们preOrder 和 inOrder 两个遍历array,让我们建立二叉树。先来举一个例子,让我们看一下preOrder 和 inOrder的特性。
/ \
/ \ / \
preOrder: 2 3 4 5 6 7 遍历顺序为:print, left, right
inOrder: 3 2 4 6 5 7 遍历顺序为: left, print, right
可以发现,preOrder的第一个肯定为root, 接下来都是左边的children, 在后面都是右边的children;
inOrder的root在最中间,root左边的都是left children, root 右边的都是 right children。
所以,我们可以建立一个递归helper function来帮助建立二叉树,把root 传递给 helper function, 让helper function 递归每一个root, 并且建立每一个root的left 和right child;
其中的规律就是遍历preOrder array,
从 preOrder 里拿到root点,对于每一个点: 有一个范围,来判断是不是走到最底端走完了,如果走完了就return,没走完就继续;
然后利用inOrder里的位置关系,来找到这个root点的 left child 和right child;
还要缩小这个范围,如果是left child, 那么就在inOrder里缩小到左边去,如果是right child,那么就在inOrder里缩小到右边去。
举例:拿到1的话,设1为一个点,初始范围为inOrder position [0, 6] 接着要找到它的left child 和 right child;
1的left - preOrder中1的下一个数字2, 范围缩小到inOrder position [0, 2],2在其中,设2为1的left, 递归2;
2的left - preOrder中2的下一个数字3, 范围缩小到inOrder position [0, 0],3在其中,设3为2的left, 递归3;
3的left - preOrder中3的下一个数字4, 范围缩小到inOrder position [0, -1],意味着走到底端了,返回到2;(3的right同理)
2的right - preOrder中3的下一个数字4, 范围缩小到inOrder position [2, 2],4在其中,设4为2的right,递归4;
4的left - preOrder中4的下一个数字5, 范围缩小到inOrder position [2, 1],意味着走到底端了,返回到2 (4的right同理);
2的left right 都有了,所以继续返回到1;
1的right - preOrder中4的下一个数字5, 范围缩小到InOrder position [4, 6],5在其中,设5为1的right, 递归5;
5的left - preOrder中5的下一个数字6, 范围缩小到inOrder position [4, 4],6在其中,设6为5的left, 递归6;
6 走到底端,返回到5;
5的right - preOrder中6的下一个数字7, 范围缩小到inOrder position [6, 6],7在其中,设7为5的right, 递归7;
7 走到低端,返回5;
5 left right 都有,返回1;
1 返回自己,结束。
具体细节请看code。
Java Solution:
Runtime beats 82.84%
完成日期:08/26/2017
关键词:Array, Tree
关键点:递归;利用pre-order 和 in-order 的位置关系递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{
public TreeNode buildTree(int[] preorder, int[] inorder)
{
Map<Integer, Integer> inMap = new HashMap<Integer, Integer>(); // save inorder number as key, position as value into map
for(int i=0; i<inorder.length; i++)
inMap.put(inorder[i], i); TreeNode root = helper(preorder, 0, 0, inorder.length - 1, inMap); return root;
} public TreeNode helper(int[] preorder, int preStart, int inStart, int inEnd,
Map<Integer, Integer> inMap)
{
if(inStart > inEnd)
return null; int rootVal = preorder[preStart];
TreeNode root = new TreeNode(rootVal);
int inRoot = inMap.get(rootVal); // position in inOrder /* inStart & inEnd: for left child, move inEnd to the left of root
* for right child, move inStart to the right of root */
root.left = helper(preorder, preStart + 1, inStart, inRoot - 1, inMap);
/* preStart: for right child, go to inorder to check how many left children does root have,
* add it into preorder to skip them to reach right child */
root.right = helper(preorder, preStart + (inRoot - inStart) + 1, inRoot + 1, inEnd, inMap); return root;
}
}
参考资料:
https://discuss.leetcode.com/topic/3695/my-accepted-java-solution
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)的更多相关文章
- [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 ...
- (二叉树 递归) 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 ...
- 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 ...
- 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 ...
- Java for 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 ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- LeetCode OJ: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#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...
随机推荐
- Java Sftp上传下载文件
需要使用jar包 jsch-0.1.50.jar sftp上传下载实现类 package com.bstek.transit.sftp; import java.io.File; import ja ...
- 【转】SWT/JFace的对话框
一.MessageDialog ,MessageDialog的用法很简单 MessageDialog.openInfomation(shell,title,message); ...
- python import xxx 与 from xxx import xx 模块引入的区别
有如下脚本script1.py: A='aaaa'B='bbbb'C='cccc'print A,B,C 1.命令行交互模式下使用import 导入方式1: >>>import sc ...
- win7系统Myeclipse下切换SVN用户
Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能.但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号.密码保存之后 ...
- Swiper.js使用方法
<!DOCTYPE html> <html> <head> ... <link rel="stylesheet" href=" ...
- 教育,创新,提升:Indiegogo和Kickstarter上受中国用户支持的10个众筹项目
中国的经济正在迅速发展,已成为世界第二大经济体.中国家庭随着经济水平的提高,越来越多父母愿意将自己的子女送到海外留学. 家长们希望自己的子女可以有机会接受国外大学优质的教育, 以便他们将来可以学成归来 ...
- Spring事务源码阅读笔记
1. 背景 本文主要介绍Spring声明式事务的实现原理及源码.对一些工作中的案例与事务源码中的参数进行总结. 2. 基本概念 2.1 基本名词解释 名词 概念 PlatformTransaction ...
- 搭建dubbo+zookeeper+dubboadmin分布式服务框架(windows平台下)
1.zookeeper注册中心的配置安装 1.1 下载zookeeper包(zookeeper-3.4.6.tar.gz),ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Goo ...
- Asp数据转Json
需要引用的文件: json.asp(可在JSON官网下载,也可在底部链接的demo中直接拷贝该文件) Conn.asp是链接数据库文件 <%@LANGUAGE="%> <% ...
- 英特尔:不再公布PC处理器多核睿频数据
据了解,以往英特尔官方有三个频率数据:基础主频:Turbo 2.0(多核)频率:以及Turbo 3.0(单核)频率.现在被隐匿的则是Turbo 2.0(多核)频率. 对此,英特尔在回应媒体时表示,给出 ...