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 (用先序和中序树遍历来建立二叉树)的更多相关文章

  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 由前序和中序遍历建立二叉树 C++

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

  5. 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 ...

  6. [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...

  7. 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 ...

  8. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

  9. leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树

    不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...

随机推荐

  1. 个人-GIT使用方法

    团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 1.Git库中由三部分组成        Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索 ...

  2. JSP-页面跳转大全

    转自:http://blog.sina.com.cn/s/blog_8c38b8b701013zzz.html (1). forward()方法 使用到javax.servlet.RequestDis ...

  3. HDU 6092`Rikka with Subset 01背包变形

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. Android + HTML5 混合开发

    摘要: 对于 Android + HTML5 混合开发以下的观点仅仅是我的个人观点,如果有什么不对的地方请指正 简介: 混合开发的 App(Android + HTML5)就是在一个 App 中内嵌一 ...

  5. 深入理解计算机系统chapter7

    链接:将各种代码和数据部分收集起来并组合成为单一文件的过程,这个文件可被加载到存储器并执行. 在运行时,和一个在存储器中的程序链接起来 二.静态链接库与动态链接库 静态连接库就是把(lib)文件中用到 ...

  6. Maven在Windows中的配置以及IDE中的项目创建

    Maven在Windows下的配置 1.Maven下载地址:http://maven.apache.org/download.cgi,下载红框里的版本即可. 2.解压到D盘: 3.修改配置文件sett ...

  7. 第6章 Overlapped I/O, 在你身后变戏法 ---1

    这一章描述如何使用 overlapped I/O(也就是 asynchronous I/O).某些时候 overlapped I/O 可以取代多线程的功用.然而,overlapped I/O 加上co ...

  8. 关于SSH

    SSH的英文全称是Secure Shell. 传统的网络服务程序,如:ftp和telnet在本质上都是不安全安全安全安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令 ...

  9. PHP常用数组(Array)函数整理

    整理了一份PHP开发中数组操作大全,包含有数组操作的基本函数.数组的分段和填充.数组与栈.数组与列队.回调函数.排序.计算.其他的数组函数等. 一.数组操作的基本函数 数组的键名和值 array_va ...

  10. hdu 4778 Gems Fight! 状态压缩DP

    Gems Fight! Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)T ...