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. 开源管理系统OSSIM设置 语言为中文简体

    最近研究OSSIM系统,OSSIM的安装是做好的ISO,操作系统选择的是CentOS 64Bit系统.我使用的OSSIM 4.11 的ISO安装,虽然系统说明支持中文,实际上,只是台湾的繁体中文而以. ...

  2. Atitit.java jna  调用c  c++ dll的原理与实践  总结  v2  q27

    Atitit.java jna  调用c  c++ dll的原理与实践  总结  v2  q27 1. Jna简单介绍1 2. Jna范例halo owrld1 3. Jna概念2 3.1. (1)需 ...

  3. 转:SQL2008 UNPIVOT 列转行示例

    CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int, Emp3 int, Emp4 int, Emp5 int); GO INSERT INTO pv ...

  4. js 把字符串当做方法执行

    <SCRIPT LANGUAGE="JavaScript"> function test(str){ alert(str); } eval('test("aa ...

  5. posix正则表达式说明

    转载自:http://baiy.cn/utils/_regex_doc/index.htm 正则表达式说明 简介 大体来讲,正则表达式的文法分为3种标准:BRE.ERE 和 ARE.其中 BER 和 ...

  6. oracle密码过期解决方法

    Oracle提示错误消息ORA-28001: the password has expired 在oracle服务器上用sqlplus / as sysdba登录进去,可以通过下面的sql语句查看账户 ...

  7. CSS基础4——使用CSS格式化元素内容的文本

    CSS的文本属性用于控制文本的段落格式,如设置首行缩进.段落对齐方式.字间距.行间距等. 1.设置文本首行缩进:text-indent 可选属性值包含: 长度 / 百分比 2.设置文本对齐方式:tex ...

  8. JavaScript入门:004—JS凝视的写法和基本运算符

    JS的凝视JS中加凝视和寻常写C#代码是几乎相同的.有//和/* */这两种.单行凝视使用双斜杠比如. <script type="text/javascript"> ...

  9. 打印99乘法表-python

    题目:如何打印出阶梯状的99乘法表? 题解: #coding:utf-8def multiplication_tables(num):#for i in range(1,10): for j in r ...

  10. PMP杂谈--项目组织,矩阵组织,职能型组织,复合型组织

    (1)项目组织的优缺点:优:项目经理权力大:缺:解散时焦虑.没有提拔权力,不利于专业技术积累,设备反复.难以保证资源的充分利用. (2)矩阵组织的优缺点:优:资源利用率高,横向信息沟通.项目经理权力提 ...