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

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

There is an example.
        _______7______
/ \
__10__ ___2
/ \ /
4 3 _8
\ /
1 11
The preorder and inorder traversals for the binary tree above is:
preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}

The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder:  {7}, {10,4,3,1}, {2,8,11}
inorder:     {4,10,3,1}, {7}, {11, 8,2}

        _______7______
/ \
{4,10,3,1} {11,8,2}
Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right
part agin based on the preorder.

2nd round
left part                                                                            right part
preorder: {10}, {4}, {3,1}                                              {2}, {8,11}
inorder:  {4}, {10}, {3,1}                                                {11,8}, {2}

        _______7______
/ \
__10__ ___2
/ \ /
4 {3,1} {11,8}
see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.

Same way to split {3,1} and {11,8}, yo will get the complete tree now.

        _______7______
/ \
__10__ ___2
/ \ /
4 3 _8
\ /
1 11
So, simulate this process from bottom to top with recursion as following code.

c++

TreeNode *BuildTreePI(
vector<int> &preorder,
vector<int> &inorder,
int p_s, int p_e,
int i_s, int i_e){
if(p_s > p_e) return NULL;
int pivot = preorder[p_s];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i] == pivot)
break;
}
int length1 = i-i_s-1;
int length2 = i_e-i-1;
TreeNode* node = new TreeNode(pivot);
node->left = BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s, i-1);
node->right = BuildTreePI(preorder, inorder, p_e-length2, p_e, i+1, i_e);
return node;
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
}

java

public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildPI(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
}
public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){
if(p_s>p_e)
return null;
int pivot = preorder[p_s];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i]==pivot)
break;
}
TreeNode node = new TreeNode(pivot);
int lenLeft = i-i_s;
node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);
node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);
return node;
}

[LeetCode-21]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 (用先序和中序树遍历来建立二叉树)

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 提升效率的JQUERY(转)

    摘要 本文部分整理了JQuery性能提升的一些方法,内容综合自artzstudio,viralpatel,htmlgoodies等网站,希望对大家有所帮助.这些规则虽然简单,但如果不遵循就会影响程序执 ...

  2. Yum Error Another app is currently holding the yum lock; waiting for it to exit

    Another app is currently holding the yum lock; waiting for it to exit... The other application is: P ...

  3. nginx源码学习_数据结构(ngx_int_t)

    nginx中关于整型的数据结构位于src/core/ngx_config.h中 结构比较简单,就是一个typedef的操作,具体如下: typedef intptr_t ngx_int_t; type ...

  4. 内核并发管理---spin lock

    自旋锁最初是为了在smp系统上使用而设计. 1.在单处理器非抢占模式下,自旋锁不做任何事情. #ifdef CONFIG_PREEMPT_COUNT     //支持抢占模式 #define pree ...

  5. centos7+nginx部署asp.net core mvc网站

    1. 安装 .net core 可参见官网安装教程. 选择Linux发行版本为Centos/Oracle 添加dotnet的yum仓库配置 $ sudo rpm -Uvh https://packag ...

  6. [转]成员函数指针与高性能的C++委托

    原文(作者:Don Clugston):Member Function Pointers and the Fastest Possible C++ Delegates 译文(作者:周翔): 成员函数指 ...

  7. 域名出售(www.shopbao.com)

    www.shopbao.com 前有淘宝,今有商宝. 商宝网站,精彩无限. 因经济原因,忍痛割爱,欲出售该商业域名. 有意者,请联系:18610310405 MAIL: jieisme@163.com

  8. Blue Jeans - poj 3080(后缀数组)

    大致题意: 给出n个长度为60的DNA基因(A腺嘌呤 G鸟嘌呤 T胸腺嘧啶 C胞嘧啶)序列,求出他们的最长公共子序列 使用后缀数组解决 #include<stdio.h> #include ...

  9. opus 规范 与参数解析

    bytestream_put_buffer(&p, "OpusHead", 8); bytestream_put_byte(&p, 1); /* Version * ...

  10. Servlet线程安全 Filter http://zwchen.iteye.com/blog/91088

    概述 在探讨java线程安全前,让我们先简要介绍一下Java语言. 任何语言,如C++,C#,Java,它们都有相通之处,特别是语法,但如果有人问你,Java语言的核心是什么?类库?关键字?语法?似乎 ...