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 ...
随机推荐
- mybatis-分页显示数据
分页用到的两个实体类 package com.yangwei.shop.entity; /** * 注意 get,set,remove 方法与一般的实体类的不同*/ public class Syst ...
- 基于jquery开发的UI框架整理分析
根据调查得知,现在市场中的UI框架差不多40个左右,不知大家都习惯性的用哪个框架,现在市场中有几款UI框架稍微的成熟一些,也是大家比较喜欢的一种UI框架,那应该是jQuery,有部分UI框架都是根据j ...
- markdown编辑器的学习
markdown编辑器的学习 1 标题 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 2列表 无序列表 1 2 3 4 有序列表 1 2 3 4 3引用 这里是引用,哈哈我也不知道到我引 ...
- JAVA多线程--Thinking in java
聊聊并发:http://ifeve.com/java-concurrency-thread-directory/ 阻塞状态: sleep 可中断利用 interrupt方法 wait IO 不可中 ...
- 为什么Java中的String类是不可变的?
String类是Java中的一个不可变类(immutable class). 简单来说,不可变类就是实例在被创建之后不可修改. 在<Effective Java> Item 15 中提到了 ...
- mxnet的训练过程——从python到C++
mxnet的训练过程--从python到C++ mxnet(github-mxnet)的python接口相当完善,我们可以完全不看C++的代码就能直接训练模型,如果我们要学习它的C++的代码,从pyt ...
- GCD hdu2588
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Problem 2144 Shooting Game fzu
Problem 2144 Shooting Game Accept: 99 Submit: 465Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- poj3468树状数组的区间更新,区间求和
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 47174 ...
- [SDOI2009]晨跑
又是一道山东省选的题目,居然题目又十分水100行的代码就随随便便AC了. Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止 ...